nansum
(Not recommended) Sum, ignoring NaN
values
nansum
is not recommended. Use the MATLAB® function sum
instead. With the sum
function, you can specify whether
to include or omit NaN
values for the calculation. For more information,
see Version History.
Description
returns the
y
= nansum(X
)sum
of the elements of
X
, computed after removing all NaN
values.
If
X
is a vector, thennansum(X)
is the sum of all the non-NaN
elements ofX
.If
X
is a matrix, thennansum(X)
is a row vector of column sums, computed after removingNaN
values.If
X
is a multidimensional array, thennansum
operates along the first nonsingleton dimension ofX
. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.nansum
removes allNaN
values.
For information on how nansum
treats arrays of all
NaN
values, see Tips.
returns the sum over the dimensions specified in the vector y
= nansum(X
,vecdim
)vecdim
. The
function computes the sums after removing NaN
values. For example, if
X
is a matrix, then nansum(X,[1 2])
is the sum of
all non-NaN
elements of X
because every element of a
matrix is contained in the array slice defined by dimensions 1 and 2.
Examples
Input Arguments
Output Arguments
Tips
When
nansum
computes the sum of an array of allNaN
values, the array is empty once theNaN
values are removed and, therefore, the sum of the remaining elements is0
. The output0
is not a sum ofNaN
values.