user238469
user238469

Reputation:

Matlab's nanmean( ) function not working with dimensions other than 1

Take this example from the mathworks help of nanmean():

 X = magic(3);
X([1 6:9]) = repmat(NaN,1,5)

X =

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN

>> y = nanmean(X,2)
??? Error using ==> nanmean
Too many input arguments.

Why is it showing error even when the docs say the mean can be taken in any dimension dim of X as y = nanmean(X,dim)? Thanks.

Upvotes: 3

Views: 3254

Answers (2)

mwengler
mwengler

Reputation: 2778

I run exactly the code you have and I get no error. In particlar here is what I ran:

>> X = magic(3);
X([1 6:9]) = repmat(NaN,1,5)

X =

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN

>> y = nanmean(X,2)

y =

     1
     4
     4


>> which nanmean
C:\Program Files\MATLAB\R2010b\toolbox\stats\stats\nanmean.m

The only thing I can think of is that you have a different version of nanmean.m on your path. Try a which nanmean and see if it points into the stats toolbox.

Upvotes: 1

TJ1
TJ1

Reputation: 8498

here is the reason:

If X contains a vector of all NaN values along some dimension, the vector is empty once the NaN values are removed, so the sum of the remaining elements is 0. Since the mean involves division by 0, its value is NaN. The output NaN is not a mean of NaN values.

Look at: http://www.mathworks.com/help/toolbox/stats/nanmean.html

Upvotes: 1

Related Questions