Quantcast

Documentation Center

  • Trial Software
  • Product Updates

mean

Average or mean value of array

Syntax

Description

example

M = mean(A) returns the mean value along the first array dimension of A whose size does not equal 1.

  • If A is a vector, then mean(A) returns the mean of the elements.

  • If A is a nonempty, nonvector matrix, then mean(A) treats the columns of A as vectors and returns a row vector whose elements are the mean of each column.

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

  • If A is a multidimensional array, then mean(A) treats the values along the first array dimension whose size does not equal 1 as vectors and returns an array of row vectors. The size of 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(___,type) returns the mean in the class specified by type, using any of the input arguments in the previous syntaxes. type can be 'double', 'native', or 'default'.

Examples

expand all

Mean of Matrix Columns

Define a 4-by-3 matrix.

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

Find the mean value of each column.

M = mean(A)
M =

    1.7500    2.2500    1.7500

Mean of Matrix Rows

Define a 2-by-3 matrix.

A = [0 1 1; 2 3 2]
A =

     0     1     1
     2     3     2

Find the mean value of each row.

M = mean(A,2)
M =

    0.6667
    2.3333

Mean of 3-D array

Create a 1-by-3-by-4 array of integers between 1 and 10.

A = gallery('integerdata',10,[1,3,4],1)
A(:,:,1) =

    10     8    10


A(:,:,2) =

     6     9     5


A(:,:,3) =

     9     6     1


A(:,:,4) =

     4     9     5

Find the mean values of this 3-D array along the second dimension.

M = mean(A)
M(:,:,1) =

    9.3333


M(:,:,2) =

    6.6667


M(:,:,3) =

    5.3333


M(:,:,4) =

     6

This operation produces a 1-by-1-by-4 array by computing the mean of the three values along the second dimension. The size of the second dimension is reduced to 1.

Compute the mean along the first dimension of A.

M = mean(A,1);
isequal(A,M)
ans =

     1

This operation returns the same array as A because the size of the first dimension is 1.

Mean of 8-bit Integer Array

Define a 1-by-4 vector of 8-bit integers.

A = int8(1:4)
A =

    1    2    3    4

Compute the mean value.

M = mean(A),
class(M)
M =

    2.5000


ans =

double

M is the mean value returned as double. This operation returns the same result as M = mean(A,'default').

Specify type as 'native' to return the mean value as an 8–bit integer.

M = mean(A,'native'),
class(M)
M =

    3


ans =

int8

Input Arguments

expand all

A — Input arrayvector | matrix | multidimensional array

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

If A contains NaN, then M returns NaN.

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

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, 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 if dim is greater than ndims(A).

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

type — Output class'default' (default) | 'double' | 'native'

Output class, specified as 'default', 'double', or 'native', and which defines the data type of the output, M.

typeOutput data type
'default'double, unless the input data type is single. In which case, the output data type is single.
'double'double
'native'same data type as the input array, A

See Also

| | | | | | |

Was this topic helpful?