Possible Duplicate:
Dealing with NaN’s in matlab functions
Is there a one line command that allows you to take the elementwise average of a matrix (ignoring NaN
's) in Matlab? For example,
>> A = [1 0 NaN; 0 3 4; 0 NaN 2]
A =
1 0 NaN
0 3 4
0 NaN 2
So the mean(A)
should equal (1+3+2+4+0+0+0)/7 = 1.4286
Also, I don't have access to the stats toolbox so I cannot use nanmean()
You can use isnan()
to filter out the unwanted elements:
mean(A(~isnan(A)))
nanmean
Performs just like mean
, but ignoring nans.
For example:
>> A = [1 0 NaN; 0 3 4; 0 NaN 2]
A =
1 0 NaN
0 3 4
0 NaN 2
>> nanmean(A)
ans =
0.333333333333333 1.5 3
>> nanmean(A,2)
ans =
0.5
2.33333333333333
1
>> nanmean(A(:))
ans =
1.42857142857143
来源:https://stackoverflow.com/questions/14694706/taking-the-mean-of-a-matrix-with-nans-in-matlab