Averaging multi-dimensional arrays across several dimensions at once
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
James Tursa
le 8 Juin 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).
Réponse acceptée
Jan
le 9 Juin 2016
R = mean(reshape(X(1, 1, :, :, :), 1, []))
2 commentaires
Jason Stockton
le 9 Août 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.
Stephen23
le 10 Août 2021
"...I needed to find the mean across all dimensions."
mean(X(:))
mean(X,'all')
Plus de réponses (2)
Stephen23
le 9 Juin 2016
Modifié(e) : Stephen23
le 9 Juin 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.
1 commentaire
dpb
le 8 Juin 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.
1 commentaire
Walter Roberson
le 9 Juin 2016
squeeze() is not going to benefit if you are going to (:) afterwards.
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!