nanmean

Mean ignoring NaN values

Syntax

y = nanmean(X)
y = nanmean(X,dim)

Description

y = nanmean(X) is the mean of X, computed after removing NaN values.

For vectors x, nanmean(x) is the mean of the remaining elements, once NaN values are removed. For matrices X, nanmean(X) is a row vector of column means, once NaN values are removed. For multidimensional arrays X, nanmean operates along the first nonsingleton dimension.

y = nanmean(X,dim) takes the mean along dimension dim of X.

    Note:   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.

Examples

Find column means for data with missing values:

X = magic(3);
X([1 6:9]) = repmat(NaN,1,5)
X =
   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN
y = nanmean(X)
y =
   3.5000  3.0000  NaN

See Also

| |

Was this topic helpful?