This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this 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.


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

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also


Introduced before R2006a

Was this topic helpful?