Averaging multi-dimensional arrays across several dimensions at once
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2016-6-8
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).
采纳的回答
Jan
2016-6-9
R = mean(reshape(X(1, 1, :, :, :), 1, []))
2 个评论
Jason Stockton
2021-8-9
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
2021-8-10
"...I needed to find the mean across all dimensions."
mean(X(:))
mean(X,'all')
更多回答(2 个)
Stephen23
2016-6-9
编辑:Stephen23
2016-6-9
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
2016-6-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!