How to Get Average of Array With the mean() Function in Matlab

Ammar Ali Feb 12, 2024
  1. Get the Mean or Average of an Array in MATLAB Using the mean() Function
  2. Get the Mean or Average of an Array in MATLAB Using the Sum and Divide Method
  3. Get the Mean or Average of an Array in MATLAB Using a Loop
  4. Conclusion
How to Get Average of Array With the mean() Function in Matlab

MATLAB, a powerful numerical computing environment, is widely used in various scientific and engineering disciplines for data analysis and visualization. One common task in data analysis is finding the mean or average of an array.

Whether you are dealing with a set of experimental measurements or a dataset, MATLAB provides simple and efficient functions to calculate the mean.

In this article, we will explore different methods to find the mean of an array in MATLAB.

Get the Mean or Average of an Array in MATLAB Using the mean() Function

MATLAB provides a powerful and straightforward method to calculate the mean or average of an array through the built-in mean() function.

The mean() function in MATLAB is used to compute the average of elements in an array. Its basic syntax is as follows:

result = mean(A, dim, 'native');

Where:

  • A: The input array whose mean is to be calculated.
  • dim (optional): Specifies the dimension along which the mean is computed. If omitted, the mean is calculated over the entire array.
  • 'native' (optional): Specifies the data type of the output. Options include 'double', 'single', and others. If not specified, MATLAB uses the default data type.

Now, let’s delve into various scenarios with code examples.

Example 1: Finding the Mean of a Vector

% Vector Example
vector = [1, 3, 5];
result = mean(vector)

In this example, we begin by defining a vector named vector with values [1, 3, 5]. The objective is to find the mean of this vector using the mean() function.

The function is applied to the vector without specifying any dimension, which implies that it will calculate the mean across the entire vector. The result is stored in the variable result.

Output:

result =

     3

Example 2: Calculating Column Averages in a Matrix

% Matrix Example - Column Averages
matrix = [1 3 5; 2 3 6];
result = mean(matrix)

Here, we work with a matrix named matrix consisting of two rows and three columns ([1 3 5; 2 3 6]). The mean() function is applied to the matrix without specifying a dimension, which defaults to computing column averages.

The output variable result is a row vector containing the mean of each column. The displayed output reveals the computed averages for each column.

Output:

result =

   1.5000   3.0000   5.5000

Example 3: Computing Row Averages in a Matrix

% Matrix Example - Row Averages
matrix = [1 3 5; 2 3 6];
result = mean(matrix, 2)

In this example, the goal is to calculate the row averages of a matrix. The matrix matrix is the same as in the previous example.

However, this time, the mean() function is used with the second argument set to 2, indicating that the computation should be along the rows. The output variable result is a column vector containing the mean of each row.

Output:

result =

   3.0000
   3.6667

Example 4: Specifying Output Data Type

% Matrix Example - Specifying Output Data Type
matrix = [1 3 5; 2 3 6];
result = mean(matrix, 'double')

Here, we explore the ability to specify the output data type of the mean() function. A matrix matrix is used, and the output data type is explicitly set to double.

The resulting result variable contains the mean of each column with the specified data type. The output confirms that the mean values are presented in double precision.

Output:

result =

   1.5000   3.0000   5.5000

These examples demonstrate the versatility of the mean() function in MATLAB for computing averages, whether dealing with vectors, matrices or specifying output data types.

Get the Mean or Average of an Array in MATLAB Using the Sum and Divide Method

In MATLAB, an alternative method for calculating the mean or average of an array involves using the sum and divide approach. This method relies on the fundamental formula that the average equals the sum of the elements divided by the number of elements.

The sum and divide method is based on two essential functions in MATLAB: sum() and numel() (or length()).

The sum() function computes the sum of all elements in an array, and the numel() or length() function determines the number of elements. Dividing the sum by the number of elements yields the average.

The syntax for this method is as follows:

result = sum(A) / numel(A);

Here, A is the input array for which we want to find the average. This method is particularly useful when you prefer an alternative to the built-in mean() function.

Now, let’s explore this method with several examples.

Example 1: Finding the Mean of a Vector

% Vector Example
vector = [1, 3, 5];
result = sum(vector) / numel(vector)

In this example, a vector named vector is defined with values [1, 3, 5]. The sum() function calculates the sum of these elements, and numel(vector) determines the number of elements in the vector.

Dividing the sum by the number of elements yields the average, which is stored in the variable result.

Output:

result =

     3

Example 2: Calculating Column Averages in a Matrix

% Matrix Example - Column Averages
matrix = [1 3 5; 2 3 6];
result = sum(matrix) / numel(matrix)

This example involves a matrix named matrix with two rows and three columns. The sum() function computes the sum of all elements in the matrix, and numel(matrix) determines the total number of elements.

Dividing the sum by the number of elements results in the average. The variable result stores the calculated average.

Output:

result =

    0.5000    1.0000    1.8333

Example 3: Computing Row Averages in a Matrix

% Matrix Example - Row Averages
matrix = [1 3 5; 2 3 6];
result = sum(matrix, 2) / numel(matrix(1, :))

In this case, we calculate row averages in a matrix. The sum(matrix, 2) computes the sum along each row, and numel(matrix(1, :)) determines the number of elements in each row.

Dividing the row sum by the number of elements provides the row averages. The variable result stores the computed row averages.

Output:

result =

    3.0000
    3.6667

These examples demonstrate the sum and divide method’s flexibility in calculating the mean or average of arrays in MATLAB.

Get the Mean or Average of an Array in MATLAB Using a Loop

Another approach to calculating the mean or average of an array in MATLAB involves using a loop. This method is more manual than the built-in functions but can be insightful for understanding the underlying logic.

The loop method involves traversing through each element of the array using a loop, accumulating the sum of elements, and then dividing by the total number of elements to compute the mean. The basic syntax for this method is as follows:

total = 0;
for i = 1:length(A)
    total = total + A(i);
end
result = total / length(A);

Here, A is the input array for which we want to find the average. The loop iterates through each element of the array, adding it to the total variable.

After the loop, the mean is calculated by dividing the total by the number of elements.

Now, let’s explore this method with several examples.

Example 1: Finding the Mean of a Vector

% Vector Example
vector = [1, 3, 5];
total = 0;
for i = 1:length(vector)
    total = total + vector(i);
end
result = total / length(vector)

In this example, a vector named vector is defined with values [1, 3, 5]. The loop iterates through each element of the vector, adding it to the total variable.

After the loop, the mean is calculated by dividing the total by the number of elements in the vector, which is stored in the variable result.

Output:

result =

     3

Example 2: Calculating Column Averages in a Matrix

% Matrix Example - Column Averages
matrix = [1 3 5; 2 3 6];
total = zeros(1, size(matrix, 2));
for i = 1:size(matrix, 1)
    total = total + matrix(i, :);
end
result = total / size(matrix, 1)

In this example, a matrix named matrix with two rows and three columns is used. The loop iterates through each row, accumulating the sum of the row elements in the total variable.

After the loop, the mean is calculated for each column by dividing the total by the number of rows, resulting in the variable result.

Output:

result =

   1.5000   3.0000   5.5000

Example 3: Computing Row Averages in a Matrix

% Matrix Example - Row Averages
matrix = [1 3 5; 2 3 6];
total = zeros(size(matrix, 1), 1);
for i = 1:size(matrix, 2)
    total = total + matrix(:, i);
end
result = total / size(matrix, 2)

This example calculates row averages in a matrix. The loop iterates through each column, accumulating the sum of column elements in the total variable.

After the loop, the mean is calculated for each row by dividing the total by the number of columns, resulting in the variable result.

Output:

result =

   3.0000
   3.6667

These examples illustrate how to find the mean or average of an array using a loop in MATLAB. While not as concise as built-in functions, this method provides a fundamental understanding of the mean calculation process and can be beneficial in certain scenarios.

Conclusion

MATLAB offers several versatile methods for finding the mean or average of an array, allowing us to choose the approach that best fits our specific needs.

The built-in mean() function provides a convenient and efficient solution for most cases, offering flexibility to calculate averages along specific dimensions or with specified data types. The sum and divide method offer a straightforward alternative, leveraging the sum() and numel() functions.

Additionally, a loop-based approach provides a manual but insightful way to understand the underlying logic of mean calculations. Whether opting for simplicity, precision, or educational insights, MATLAB’s diverse methods empower us to effortlessly compute the mean or average of arrays in various scenarios.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Array