This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Sum ignoring NaN values


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


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.

    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. The output 0 is not a sum of NaN values.


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


Introduced before R2006a

Was this topic helpful?