Main Content

(Not recommended) Mean, ignoring `NaN`

values

`nanmean`

is not recommended. Use the MATLAB^{®} function `mean`

instead. With the `mean`

function, you can specify
whether to include or omit `NaN`

values for the calculation. For more
information, see Compatibility Considerations.

returns
the `y`

= nanmean(`X`

)`mean`

of the elements of
`X`

, computed after removing all `NaN`

values.

If

`X`

is a vector, then`nanmean(X)`

is the mean of all the non-`NaN`

elements of`X`

.If

`X`

is a matrix, then`nanmean(X)`

is a row vector of column means, computed after removing`NaN`

values.If

`X`

is a multidimensional array, then`nanmean`

operates along the first nonsingleton dimension of`X`

. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.`nanmean`

removes all`NaN`

values.

For information on how `nanmean`

treats arrays of all
`NaN`

values, see Tips.

returns the mean over the dimensions specified in the vector `y`

= nanmean(`X`

,`vecdim`

)`vecdim`

.
The function computes the means after removing `NaN`

values. For example,
if `X`

is a matrix, then `nanmean(X,[1 2])`

is the mean
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.

When

`nanmean`

computes the mean of an array of all`NaN`

values, the array is empty once the`NaN`

values are removed and, therefore, the sum of the remaining elements is`0`

. Because the mean calculation involves division by`0`

, the mean value is`NaN`

. The output`NaN`

is not a mean of`NaN`

values.