# mean

Average or mean value of array

## Syntax

• ``M = mean(A)``
example
• ``M = mean(A,dim)``
example
• ``M = mean(___,outtype)``
example
• ``M = mean(___,nanflag)``
example

## Description

example

````M = mean(A)` returns the mean of the elements of `A` along the first array dimension whose size does not equal 1.If `A` is a vector, then `mean(A)` returns the mean of the elements.If `A` is a matrix, then `mean(A)` returns a row vector containing the mean of each column.If `A` is a multidimensional array, then `mean(A)` operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes `1` while the sizes of all other dimensions remain the same.```

example

````M = mean(A,dim)` returns the mean along dimension `dim`. For example, if `A` is a matrix, then `mean(A,2)` is a column vector containing the mean of each row.```

example

````M = mean(___,outtype)` returns the mean with a specified data type, using any of the input arguments in the previous syntaxes. `outtype` can be `'default'`, `'double'`, or `'native'`.```

example

````M = mean(___,nanflag)` specifies whether to include or omit `NaN` values from the calculation for any of the previous syntaxes. `mean(A,'includenan')` includes all `NaN` values in the calculation while `mean(A,'omitnan')` ignores them.```

## Examples

collapse all

### Mean of Matrix Columns

Create a matrix and compute the mean of each column.

```A = [0 1 1; 2 3 2; 1 3 2; 4 2 2] ```
```A = 0 1 1 2 3 2 1 3 2 4 2 2 ```
```M = mean(A) ```
```M = 1.7500 2.2500 1.7500 ```

### Mean of Matrix Rows

Create a matrix and compute the mean of each row.

```A = [0 1 1; 2 3 2] ```
```A = 0 1 1 2 3 2 ```
```M = mean(A,2) ```
```M = 0.6667 2.3333 ```

### Mean of 3-D Array

Create a 4-by-2-by-3 array of integers between 1 and 10 and compute the mean values along the second dimension.

```A = gallery('integerdata',10,[4,2,3],1); M = mean(A,2) ```
```M(:,:,1) = 9.5000 6.5000 9.5000 6.0000 M(:,:,2) = 1.5000 4.0000 7.5000 7.5000 M(:,:,3) = 7.0000 2.5000 4.0000 5.5000 ```

### Mean of Single-Precision Array

Create a single-precision vector of ones and compute its single-precision mean.

```A = single(ones(1e8,1)); M = mean(A,'native') ```
```M = 1 ```

The expected mean, 1, cannot be represented in single-precision due to saturation. Use the `double` output type to calculate the expected mean of `A`.

```M = mean(A,'double') ```
```M = 1 ```

### Mean Excluding `NaN`

Create a vector and compute its mean, excluding `NaN` values.

```A = [1 0 0 1 NaN 1 NaN 0]; M = mean(A,'omitnan') ```
```M = 0.5000 ```

If you do not specify `'omitnan'`, then `mean(A)` returns `NaN`.

## Input Arguments

collapse all

### `A` — Input arrayvector | matrix | multidimensional array

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

• If `A` is a scalar, then `mean(A)` returns `A`.

• If `A` is an empty 0-by-0 matrix, then `mean(A)` returns `NaN`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical` | `char` | `datetime` | `duration`

### `dim` — Dimension to operate alongpositive integer scalar

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

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

Consider a two-dimensional input array, `A`.

• If `dim = 1`, then `mean(A,1)` returns a row vector containing the mean of the elements in each column.

• If `dim = 2`, then `mean(A,2)` returns a column vector containing the mean of the elements in each row.

`mean` returns `A` when `dim` is greater than `ndims(A)` or when `size(A,dim)` is `1`.

Data Types: `double` | `single` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `outtype` — Output data type`'default'` (default) | `'double'` | `'native'`

Output data type, specified as `'default'`, `'double'`, or `'native'`. These options also specify the data type in which the operation is performed.

`outtype`Output data type
`'default'``double`, unless the input data type is `single`, `duration`, or `datetime`, in which case, the output is `'native'`
`'double'``double`, unless the data type is `duration` or `datetime`, in which case, `'double'` is not supported
`'native'`same data type as the input, unless
• Input data type is `logical`, in which case, the output is `double`

• Input data type is `char`, in which case, `'native'`is not supported

Data Types: `char`

### `nanflag` — `NaN` condition‘`includenan`' (default) | ‘`omitnan`'

`NaN` condition, specified as one of these values:

• `'includenan'` — Include `NaN` values when computing the mean, resulting in `NaN`.

• `'omitnan'` — Ignore all `NaN` values in the input.

The `mean` function does not support the `nanflag` option for `datetime` or `duration` arrays.

Data Types: `char`

expand all

### Mean

For a random variable vector A made up of N scalar observations, the mean is defined as

$\mu =\frac{1}{N}\sum _{i=1}^{N}{A}_{i}.$