| by Arround The Web | No comments

Average in MATLAB (Mean Function)

The mean is a value obtained by dividing the sum of the elements by the total number of elements. Manually, if we handle an extensive data set, it is very difficult and time taking to calculate the mean. But, with the help of mean() function, you can quickly compute the average of such a large data set.

In this article, we will go through the MATLAB mean() function by elaborating it using simple and practical examples.

How to Use mean() Function in MATLAB?

The mean() function in MATLAB is a handy tool for finding the average value of elements in an array. You can choose a specific dimension along which the mean should be calculated, or you can let MATLAB automatically determine it for you. If you do not specify a dimension, MATLAB calculates the mean along the first non-singleton dimension of the array, giving you the average value you are looking for.

Syntax

The mean() function has different syntaxes given below, and each syntax works differently.

mean(x)
mean(x,"all")
mean(x,dim)
mean(x,vecdim)
mean(x_,outtype)
mean(x,missingflag)

 
Here, mean(x) returns the average value for all x components along the first array dimension with a size greater than 1.

    • The average of all the x elements is returned when x is a vector.
    • When x is a matrix, mean(x) gives a row vector that contains the means of all the columns.

mean(x,”all”) provides the average value of all x elements.

The result of the mean(x,dim) is the mean along dim. For example, mean(x,2) gives a column vector holding the average of each row if x is a matrix.

mean(x,vecdim) provides an average depending on the dimensions in the vector vecdim. If x is a matrix, mean(x,[1 2]) provides the average of all x elements since each member in the matrix is located within the array slice having dimensions 1 and 2.

The function mean(x,outtype) returns the mean for any of the previously mentioned syntaxes with the given data type. “default,” “double,” or “native” are possible outtypes.

The function mean(x,missingflag) indicates whether to include missing values in x. For example, mean(x,”omitmissing”) computes the mean ignoring all missing values. The mean() function includes missing values by default.

Example 1

This example simply creates a vector and computes the average of all components by using mean() function.

x = [2:4:50];
result = mean(x)

 

Example 2

This example simply creates a matrix and computes the average of each column by using the mean() function.

x = [1:2:10; ones(1,5); 7.9 6 4.06 3.21 0.001];
result = mean(x)

 

Example 3

This example simply creates a matrix and computes the average of each row by using the mean() function.

x = [1:2:10; ones(1,5); 7.9 6 4.06 3.21 0.001];
result = mean(x, 2)

 

Example 4

This example simply creates a matrix and computes the average of all matrix components by using the mean() function.

x = [1:2:10; ones(1,5); 7.9 6 4.06 3.21 0.001];
result = mean(x, "all")

 

Example 5

In this example, we use another way to calculate the average of all matrix elements, by using the mean() function.

x = [1:2:10; ones(1,5); 7.9 6 4.06 3.21 0.001];
result = mean(x, [1 2])

 

Example 6

This MATLAB code simply creates a matrix and calculates the mean by mentioning the native data type (default data type).

x = [1:2:10; ones(1,5); 7.9 6 4.06 3.21 0.001];
result = mean(x, "native")

 

Example 7

The given MATLAB code calculates the mean of all column entries except the NaN values.

x = [1:2:10; ones(1,5); 7.9 NaN 4.06 3.21 NaN];
result = mean(x, "omitmissing")

 

Conclusion

MATLAB’s built-in mean() function is a useful tool to find the average of any data collection. The data collection can be stored in a vector or a matrix to compute the average. There are multiple ways of computing the average of a vector or a matrix. This tutorial illustrated the mean() function by explaining all the possible ways to use it in MATLAB.

Share Button

Source: linuxhint.com

Leave a Reply