Main Content

mean

Average or mean value of array

Description

example

M = mean(A)returns themeanof the elements ofAalong the first array dimension whose size does not equal 1.

  • IfAis a vector, thenmean(A)returns the mean of the elements.

  • IfAis a matrix, thenmean(A)returns a row vector containing the mean of each column.

  • IfAis a multidimensional array, thenmean(A)operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This size of this dimension becomes1while the sizes of all other dimensions remain the same asA.

example

M = mean(A,'all')computes the mean over all elements ofA. This syntax is valid for MATLAB®versions R2018b and later.

example

M = mean(A,dim)returns the mean along dimensiondim. For example, ifAis a matrix, thenmean(A,2)is a column vector containing the mean of each row.

example

M = mean(A,vecdim)computes the mean based on the dimensions specified in the vectorvecdim. For example, ifAis a matrix, thenmean(A,[1 2])is the mean of all elements inA, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

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

example

M = mean(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.mean(A,'includenan')includes allNaNvalues in the calculation whilemean(A,'omitnan')ignores them.

Examples

collapse all

Create a matrix and compute the mean of each column.

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

Create a matrix and compute the mean of each row.

A = [0 1 1; 2 3 2; 3 0 1; 1 2 3]
A =4×30 1 1 2 3 2 3 0 1 1 2 3
M = mean(A,2)
M =4×10.6667 2.3333 1.3333 2.0000

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

rng('default') A = randi(10,[4,2,3]); M = mean(A,2)
M = M(:,:,1) = 8.0000 5.5000 2.5000 8.0000 M(:,:,2) = 10.0000 7.5000 5.5000 6.0000 M(:,:,3) = 6.0000 5.5000 8.5000 10.0000

Create a 3-D array and compute the mean over each page of data (rows and columns).

A(:,:,1) = [2 4; -2 1]; A(:,:,2) = [9 13; -5 7]; A(:,:,3) = [4 4; 8 -3]; M1 = mean(A,[1 2])
M1 = M1(:,:,1) = 1.2500 M1(:,:,2) = 6 M1(:,:,3) = 3.2500

Starting in R2018b, to compute the mean over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the'all'option.

M2 = mean(A,[1 2 3])
M2 = 3.5000
Mall = mean(A,'all')
Mall = 3.5000

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

A = single(ones(10,1)); M = mean(A,'native')
M =single1

The result is also in single precision.

class(M)
ans = 'single'

Create a vector and compute its mean, excludingNaNvalues.

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

If you do not specify'omitnan', thenmean(A)returnsNaN.

Input Arguments

collapse all

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

  • IfAis a scalar, thenmean(A)returnsA.

  • IfAis an empty 0-by-0 matrix, thenmean(A)returnsNaN.

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

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimensiondimindicates the dimension whose length reduces to1. Thesize(M,dim)is1, while the sizes of all other dimensions remain the same.

Consider anm-by-ninput matrix,A:

  • mean(A,1)computes the mean of the elements in each column ofAand returns a1-by-nrow vector.

    mean(A,1) column-wise operation

  • mean(A,2)computes the mean of the elements in each row ofAand returns anm-by-1column vector.

    mean(A,2) row-wise operation

meanreturnsAwhendimis greater thanndims(A)or whensize(A,dim)is1.

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

Consider a 2-by-3-by-3 input array,A. Thenmean(A,[1 2])returns a 1-by-1-by-3 array whose elements are the means over each page ofA.

Mapping of a 2-by-3-by-3 input array to a 1-by-1-by-3 output array

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 issingle,duration, ordatetime, in which case, the output is'native'
'double' double, unless the data type isdurationordatetime, in which case,'double'is not supported
'native'

Same data type as the input, unless

  • Input data type islogical, in which case, the output isdouble

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

Data Types:char

NaNcondition, specified as one of these values:

  • “includenan”— IncludeNaNvalues when computing the mean, resulting inNaN.

  • 'omitnan'— Ignore allNaNvalues in the input.

Fordatetimearrays, you can also use'omitnat'or'includenat'to omit and includeNaTvalues, respectively.

Data Types:char

More About

collapse all

Mean

For a random variable vectorAmade up ofNscalar observations, the mean is defined as

μ = 1 N i = 1 N A i .

Extended Capabilities

版本历史

Introduced before R2006a

See Also

||||