HOWTO · MATLAB

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

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

On this page

Use MATLAB’s mean function for the average of a vector or for means along a matrix dimension. For a typical two-dimensional matrix, mean(A) returns one mean per column; use mean(A,2) for one mean per row. The vector example below displays 4 and completed successfully.

Calculate an Array Mean with MATLAB’s mean Function

For a vector, call mean with the vector as its input. This complete, self-contained example uses Octave-compatible syntax and is also valid MATLAB code:

Run this example:

values = [2, 4, 6];
average = mean(values);
disp(average)
4

For a matrix A, MATLAB applies mean(A) along the first nonsingleton dimension. In an ordinary matrix, that is dimension 1, so the result is a row vector containing the column means. Use mean(A,2) to select dimension 2; this returns a column vector containing the row means. The selected dimension is reduced to one in the result, while the other dimension is retained.

You can also specify the output type. For example, mean(A,'double') requests double output while using the default dimension, and mean(A,2,'double') combines row-wise calculation with that output type. The type option does not choose the dimension.

Calculate Array Means with sum and Division

A mean is the sum of the values divided by their count. For one overall mean across every element of a matrix, flatten it with A(:) and divide sum(A(:)) by numel(A). numel counts all elements; length returns the size of the longest dimension and therefore is not a general substitute for a matrix element count.

For dimension-wise means, use a sum and a matching count. Column means can be written as sum(A,1) ./ size(A,1): each column sum is divided by the number of rows. Row means can be written as sum(A,2) ./ size(A,2): each row sum is divided by the number of columns. The divisor must count the values included in each corresponding sum.

This manual method is useful for seeing how the calculation works or adapting it. For routine calculations, mean expresses the intent more directly.

Calculate Array Means with a Loop

A loop makes the accumulation step visible. In the example, the vector total is scalar, the column totals are a row vector, and the row totals are a column vector. The column loop adds each row and divides by the number of rows; the row loop adds each column and divides by the number of columns.

values = [2, 4, 6];
vectorTotal = 0;
for k = 1:numel(values)
    vectorTotal = vectorTotal + values(k);
end
vectorMean = vectorTotal / numel(values);

A = [1, 2; 3, 4; 5, 6];
columnTotals = zeros(1, size(A, 2));
for row = 1:size(A, 1)
    columnTotals = columnTotals + A(row, :);
end
columnMeans = columnTotals / size(A, 1);

rowTotals = zeros(size(A, 1), 1);
for column = 1:size(A, 2)
    rowTotals = rowTotals + A(:, column);
end
rowMeans = rowTotals / size(A, 2);

These loop patterns are educational alternatives. For a vector, numel(values) counts all entries. For a matrix result, use the number of rows for column means or the number of columns for row means. MATLAB’s built-in mean is clearer for routine calculations.

Choosing a Method

Use mean for the usual vector, column, or row calculation: its dimension argument makes the desired result clear and avoids maintaining a separate count. Use sum with a dimension-aware divisor when you need to show or customize the arithmetic. Use a loop when learning how accumulation works or when the calculation includes additional per-element logic. In every method, make the divisor match the values included in the sum.