Matlab

# Average in MATLAB (Mean Function)

In this MATLAB article, we show you how to implement the mean() function to find the average values of a vector, the rows or columns of a matrix, or all of its elements.

Mean() provides great flexibility in both inputs and outputs, as well as in usage modes, as it allows us to specify the type of output data, omit NaN values, and easily work with any dimension in 2D or multidimensional arrays.

Next, we will look at a full description of this function, its syntax, input arguments, its outputs and its control flags. Then, we will go through several practical examples with code snippets and images showing the different ways of calling mean() on different dimensions.

MATLAB mean() Function Syntax

m = mean ( A )

m = mean ( A, 'all' )

m = mean ( A, dim )

m = mean ( A, vecdim )

m = mean ( ___, outtype )

m = mean ( ___, nanflag )

## MATLAB mean() Function Description

The MATLAB function mean() returns in “m” the average value resulting from the elements of the vector or from certain elements of the input matrix “a”. If the input argument of this function is a vector, it returns in “m” a scalar with the average of “a”. In cases where “a” is an array, mean() provides the option of using the flag ” all” to get the mean of all elements, or the mean over rows or columns and in the dimensions we specify when calling the function with the inputs “dim” and “vecdim”.

The flexibility of this function also allows us to use the “outtype” input to specify the type of data that the scalar or vector output should have, as well as the “nanflag” input to allow us to omit NaN values. Below you can see a list with all input arguments and control flags of this function and their respective meaning and usage.

a: Input vector or matrix: This is the 2D or multidimensional vector or matrix from which we want to obtain the average values.

‘all’ : Flag “all”: When we call the function with this flag, mean() returns a scalar with the average value of all elements of the array. This flag is a character string so it must be enclosed in single quotes.

Dim: It establishes the dimension of the matrix on which we are going to operate. When we call this function to get row averages, the result is a column vector where each element is the average of the respective row

 Dim =1 a a A a a A a = a a A Input Matrix a a A a a A m = m m M Output Vector

When we get column averages ( dim = 2), the result is a row vector with the averages of each column, as shown in the following figure:

 Dim =2 a a A m a a A m a = a a a m = m a a a m a a a m Input matrix Output vector

Vecdim: This is the vector of dimensions. Each element of this array specifies a dimension in the same way as “dim” if the input array is multidimensional. These values must be explicitly enclosed in square brackets and separated by commas, or implicitly represented as a vector.

outtype: Specifies what type of data the output will be.

Nanflag: Omit or include NaN results in the output arguments.

## How to Get the Average Value of a Vector with the Mean Function of MATLAB

In this example, we will use the mean() function to find the average value of a vector. To do this, we create the vector “a” with ten elements with values from 1 to 10 and call the mean() function by passing this vector as the input argument, as shown in the following fragment:

a = [ 1, 4, 5, 9, 2, 3, 3, 4, 9, 10 ];

m = mean ( a )

As we see in the MATLAB command console in the following figure, mean() in “m” returns a scalar with the average value from the elements of the vector “a”. ## How to Get the Average Value of All Elements an Array with the Input “all” of the MATLAB Function Mean()

Now, let us see how we can use the flag “all” to find the average value of all elements of an array. To do this, we create the matrix “a” with 4 x 4 elements and send it as an input argument to the mean() function along with the flag “all” separated by commas.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, 'all'

)

In this way, mean() with the flag “all” returns a scalar with the average resulting from the calculation of all values contained in the array “a”. ## How to Get the Average of Each Row Using the “dim” Input of the MATLAB Function Mean()

In this example, we will show you how to find the average of each row of a matrix using the “dim” input of this function. In this case, we will find the average of the rows of the matrix we used in the previous example. To do this, we send the matrix as the input argument and separated by commas. The value of the “dim” input, which in this case has dimension 2. Next, we will see the code fragment for this purpose.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, 2 )

As the image below shows, mean() returns a column vector where each element is the average of each row of the matrix “a”. ## How to Get the Average of Each Column Using the “dim” Input of the MATLAB Function Mean()

To obtain the average of each column of matrix “a”, we use the same call method as in the previous example but specify the dimension 1 in the input “dim”, as shown below.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, 1 )

As the image below shows, mean() returns a row vector where each element is the average of each row of the matrix “a”. ## Conclusion

Finding averages is the first step in any statistical calculation. In this Matlab article, we showed you how to use the function to find the average values of a vector or matrix in any dimension. We have also described in detail the individual input arguments for this function and shown you the various possible applications using practical examples with code snippets and images. 