nansum - Sum ignoring NaN values

Syntax

y = nansum(X)
y = nansum(X,dim)

Description

y = nansum(X) is the sum of X, computed after removing NaN values.

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

y = nansum(X,dim) takes the sum along dimension dim of X.

Example

Find column sums 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 = nansum(X)
y =
     7     6     0

See Also

NaN, sum

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS