Averaging multi-dimensional arrays across several dimensions at once

11 views (last 30 days)
Assume I have a 5-dimensional array X, and I want to compute the mean value (a scalar) of its elements across several dimensions at once, e.g. the last three dimensions given specified values for the first two.
I tried
mean(X(1, 1, :, :, :))
but that does not give me the desired result, i.e. it produces an array output rather than a scalar output.
My workaround has been to do for loops to compute the mean across each dimension, and then manually compute the mean of all these partial (marginal) means. But this is cumbersome, as it involves writing more code, which often ends up confusing me.
Is there a simple trick to make this aim achievable using a call to the
mean
function similar to the one above?
  1 Comment
James Tursa
James Tursa on 8 Jun 2016
Is this in a loop, so you are doing this for 1,1 then 2,1 then 3,1 etc? If so, it might make sense to reshape & permute X so you can take the mean only once on the entire array (limits the amount that the data is dragged through memory).

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Jun 2016
R = mean(reshape(X(1, 1, :, :, :), 1, []))
  2 Comments
Jason Stockton
Jason Stockton on 9 Aug 2021
I had a 4-D array that I needed to find the mean across all dimensions. I used:
mean(mean(mean(mean(X))));
It's really simple, but I like your solution better.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 9 Jun 2016
Edited: Stephen23 on 9 Jun 2016
Don't waste time with ugly loops when you can write neat and efficient MATLAB code:
>> A = randi(9,6,5,4,3,2); % fake data
>> P = permute(A,ndims(A):-1:1); % invert dimension order
>> Q = reshape(P,[],size(A,2),size(A,1)); merge first three dims
>> R = permute(Q,3:-1:1); % invert dimension order
>> M = mean(R,3) % mean of last dimension
M =
4.9583 5.2917 4.8333 4.8333 5.3333
4.0833 5.6667 4.1667 5.0417 4.4167
5.3750 4.5833 3.6667 4.7500 4.9167
4.2500 4.7500 4.4583 5.5417 5.4583
5.1250 5.0417 4.7500 5.1250 3.9167
5.3333 4.2500 5.0833 5.1667 5.7917
And for comparison, lets check the mean of some of locations:
>> X = A(1,1,:,:,:);
>> mean(X(:))
ans = 4.9583
>> Y = A(6,5,:,:,:);
>> mean(Y(:))
ans = 5.7917
How it works: this method simply uses reshape to merge all of those dimensions together that need to be averaged over. Because MATLAB merges dimensions in sequence lowest to highest it is necessary to rearrange the dimensions and put the last three dimensions first, which is what permute does. These three dimensions then get merged together (as the new first dimension) using reshape. Then another permute puts this new merged dimension last again, and mean is called with its optional argument to operate over this last dimension. Bingo, for every set of values (R,C,:,:,:) the mean is given in the output matrix (R,C).
If memory is a problem then P, Q and R can use the same variable name.

dpb
dpb on 8 Jun 2016
Only thing that comes to me at the moment is to use a temporary...
mX=X(1,1,:,:,:);
mX=mean(mX(:));
it might help a little in really large cases to squeeze the first result;
mX=squeeze(X(1,1,:,:,:));
don't know; didn't test on large array.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!