# nanmean

(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.

## Syntax

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

## Description

example

````y = nanmean(X)` returns the `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.```

example

````y = nanmean(X,'all')` returns the mean of all elements of `X`, computed after removing `NaN` values.```

example

````y = nanmean(X,dim)` returns the mean along the operating dimension `dim` of `X`, computed after removing `NaN` values.```

example

````y = nanmean(X,vecdim)` returns the mean over the dimensions specified in the vector `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.```

## Examples

collapse all

Find the column means for matrix data with missing values.

```X = magic(3); X([1 6:9]) = NaN```
```X = 3×3 NaN 1 NaN 3 5 NaN 4 NaN NaN ```
`y = nanmean(X)`
```y = 1×3 3.5000 3.0000 NaN ```

Find the mean of all the values in an array, ignoring missing values.

Create a 2-by-5-by-3 array `X` with some missing values.

```X = reshape(1:30,[2 5 3]); X([10:12 25]) = NaN```
```X = X(:,:,1) = 1 3 5 7 9 2 4 6 8 NaN X(:,:,2) = NaN 13 15 17 19 NaN 14 16 18 20 X(:,:,3) = 21 23 NaN 27 29 22 24 26 28 30 ```

Find the mean of the elements of `X`.

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

Find the row means for matrix data with missing values by specifying to compute the means along the second dimension.

```X = magic(3); X([1 6:9]) = NaN```
```X = 3×3 NaN 1 NaN 3 5 NaN 4 NaN NaN ```
`y = nanmean(X,2)`
```y = 3×1 1 4 4 ```

Find the mean of a multidimensional array over multiple dimensions.

Create a 2-by-5-by-3 array `X` with some missing values.

```X = reshape(1:30,[2 5 3]); X([10:12 25]) = NaN```
```X = X(:,:,1) = 1 3 5 7 9 2 4 6 8 NaN X(:,:,2) = NaN 13 15 17 19 NaN 14 16 18 20 X(:,:,3) = 21 23 NaN 27 29 22 24 26 28 30 ```

Find the mean of each page of `X` by specifying dimensions 1 and 2 as the operating dimensions.

`ypage = nanmean(X,[1 2])`
```ypage = ypage(:,:,1) = 5 ypage(:,:,2) = 16.5000 ypage(:,:,3) = 25.5556 ```

For example, `ypage(1,1,1)` is the mean of the non-`NaN` elements in `X(:,:,1)`.

Find the mean of the elements in each `X(i,:,:)` slice by specifying dimensions 2 and 3 as the operating dimensions.

`yrow = nanmean(X,[2 3])`
```yrow = 2×1 14.5385 16.7692 ```

For example, `yrow(2)` is the mean of the non-`NaN` elements in `X(2,:,:)`.

## Input Arguments

collapse all

Input data, specified as a scalar, vector, matrix, or multidimensional array.

If `X` is an empty array, then `nanmean(X)` is `NaN`. For more details, see Tips.

Data Types: `single` | `double`

Dimension to operate along, specified as a positive integer scalar. If you do not specify a value, then the default value is the first array dimension whose size does not equal 1.

`dim` indicates the dimension whose length reduces to 1. `size(y,dim)` is 1 while the sizes of all other dimensions remain the same.

Consider a two-dimensional array `X`:

• If `dim` is equal to 1, then `nanmean(X,1)` returns a row vector containing the mean for each column.

• If `dim` is equal to 2, then `nanmean(X,2)` returns a column vector containing the mean for each row.

If `dim` is greater than `ndims(X)` or if `size(X,dim)` is 1, then `nanmean` returns `X`.

Data Types: `single` | `double`

Vector of dimensions, specified as a positive integer vector. Each element of `vecdim` represents a dimension of the input array `X`. The output `y` has length 1 in the specified operating dimensions. The other dimension lengths are the same for `X` and `y`.

For example, if `X` is a 2-by-3-by-3 array, then `nanmean(X,[1 2])` returns a 1-by-1-by-3 array. Each element of the output is the mean of the elements on the corresponding page of `X`. Data Types: `single` | `double`

## Output Arguments

collapse all

Mean values, returned as a scalar, vector, matrix, or multidimensional array.

## Tips

• 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.

## Version History

Introduced before R2006a

expand all