Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Mean, ignoring `NaN`

values

`y = nanmean(X)`

`y = nanmean(X,'all')`

`y = nanmean(X,dim)`

`y = nanmean(X,vecdim)`

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.

Instead of using `nanmean`

, you can use the MATLAB^{®} function `mean`

with the input argument
`nanflag`

specified as the value `'omitnan'`

.