필터 지우기
필터 지우기

Averaging multi-dimensional arrays across several dimensions at once

조회 수: 13 (최근 30일)
z8080
z8080 2016년 6월 8일
댓글: Stephen23 2021년 8월 10일
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
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
Jan 2016년 6월 9일
R = mean(reshape(X(1, 1, :, :, :), 1, []))
  댓글 수: 2
Jason Stockton
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
Stephen23 2021년 8월 10일
"...I needed to find the mean across all dimensions."
mean(X(:))
mean(X,'all')

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephen23
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
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by