MATLAB: How to get rid of Nan when calculating mean

MATLAB and Simulink Student Suitemeannan

Hi I am trying to get rid of the nan's on my mean calculation, if I keep them I wont be able to calculate z-scores. I was using m1=nanmean(x) but when I look at the output I still have the nan's on the column.
Thanks in advance.

Best Answer

Hi Emmanuel Gonzalez-Figueroa,
Tested with Matlab 2016b:
A = [1 NaN 2 3]; % mean w/o NaN: 2, w/ NaN: NaN
mean(A,'omitnan') % nanmean does nothing else
mean(A) % includenan-flag by default set
mean(A,'includenan') % explicit flag
If you are working with matrices and you want to calculate mean-values column-wise but some columns are full of NaN values there is no way of omitting deletion of said columns (see comment by Shubham Gupta).
If you want to calculate the mean of all matrix entries, use the above command twice.
A = [1 NaN 2 3;
3 NaN 4 5];
mean(mean(A,'omitnan'),'omitnan')
Kind regards,
Robert