mean

Arithmetic mean along a dimension.

Category: Language Fundamentals > Data Types

Syntax

M = mean(A)

Mean of a vector, or of each column of a matrix.

M = mean(A, dim)

Mean along dimension dim.

M = mean(A, 'all')

Mean of all elements.

M = mean(___, nanflag)

Controls how NaN is treated: 'includenan' (default) or 'omitnan'.

Description

The mean is the sum divided by the count. One NaN in the data makes the result NaN unless 'omitnan' is given. The mean of an empty array is NaN. Integer inputs are averaged in floating point and the result is double, so mean(int8([100 100])) does not overflow; a logical input gives the fraction of true values.

For a skewed data set the median is often more representative than the mean.

Input arguments

A
Numeric or logical array, or duration or datetime array.
dim
Dimension to average along.
nanflag
'omitnan' to ignore NaN values.

Output arguments

M
The means. The averaged dimension has size 1.

Example

Means along dimensions

A = [1 2; 3 4];
mean(A)
mean(A, 2)
mean(A, 'all')
mean([1 NaN 3], 'omitnan')
mean([1 NaN 3])
Output
ans = 1×2

     2     3
ans = 2×1

    1.5000
    3.5000
ans = 2.5000
ans = 2
ans = NaN

See also

median mode std var sum