MATLAB - Mean Function



In MATLAB, the mean function is a powerful tool that helps you find the average or mean value of a set of numbers. Whether you're analyzing data, working with matrices, or simply calculating the average of a list of values, the mean function can save you time and effort.

Let us understand how to use the mean function by learning its syntax and executing a few examples.

Syntax

Following are the syntax for mean() function −

M = mean(A);
M = mean(A, "all");
M = mean(A, dim);

The detailed explanation of the syntax is as follows −

A − This is where you input the data you want to find the mean of. It can be a vector, a matrix, even a single number or multidimensional array.

M − The mean function returns the average of the input data, and this is stored in the M variable.

"all" − The string "all" specifies that you want to calculate the mean of all elements in the array A, regardless of its size and dimensions.

dim − The dim argument specifies the dimension along which you want to calculate the mean. It can be an integer, either 1 or 2, corresponding to rows or columns in the case of a 2D matrix. For higher-dimensional arrays, you can specify a dimension index.

Examples of mean() Function

Following are the examples of using mean() function on vector, matrix and multidimensional array −

Example 1: Finding the Mean of a Vector

Suppose you have a list of exam scores: [85, 92, 78, 89, 95]. To calculate the mean score, you can use the mean function as follows −

scores = [85, 92, 78, 89, 95];
average_score = mean(scores)

When you execute above code in matlab command window the output is −

>> scores = [85, 92, 78, 89, 95];
average_score = mean(scores)

average_score = 87.8000

Example 2: Mean of a given 2-D Matrix

If you have data stored in a matrix, such as a 2D array of values, you can still use the mean function to find the mean of the entire matrix. Here's an example −

data_matrix = [10, 20, 30; 40, 50, 60; 70, 80, 90];
average_value = mean(data_matrix, 'all')

The 'all' option tells MATLAB to calculate the mean of all elements in the matrix.

When you execute the same in matlab command window the output is −

>> data_matrix = [10, 20, 30; 40, 50, 60; 70, 80, 90];
average_value = mean(data_matrix, 'all')

average_value =

    50

Example 3: Calculating the Mean Along Rows and Columns of a Matrix

Let's say you have a matrix A representing student scores, with rows as students and columns as subjects −

A = [90, 85, 75; 78, 92, 88; 81, 89, 94];

To find the mean score for each student along columns, you can use:

A = [90, 85, 75; 78, 92, 88; 81, 89, 94];
M = mean(A, 2)

The output when you execute in matlab command window is −

>> A = [90, 85, 75; 78, 92, 88; 81, 89, 94];
M = mean(A, 2)

M =

   83.3333
   86.0000
   88.0000

To find the mean score for each subject along rows, you can use −

A = [90, 85, 75; 78, 92, 88; 81, 89, 94];
M = mean(A, 1)

On execution in matlab command window the output is −

>> A = [90, 85, 75; 78, 92, 88; 81, 89, 94];
M = mean(A, 1)

M =

   83.0000   88.6667   85.6667

Example 4: Calculating the Mean Along Multiple Dimensions

Suppose you have a 3D array A having random data as shown below −

A = rand(4, 3, 2);  % Create a random 3D array

To find the mean along the first and second dimensions, you can use −

A = rand(4, 3, 2);
M = mean(A, [1, 2])

When you execute the same in matlab command window the output is as follows −

>> A = rand(4, 3, 2);
M = mean(A, [1, 2])

M(:,:,1) =

    0.6139


M(:,:,2) =

    0.6624
Advertisements