フィルターのクリア

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.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 18 日
Gar comments to Stephen Cobeldick:
Very Useful!!

サインインしてコメントする。


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.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 6 月 9 日
squeeze() is not going to benefit if you are going to (:) afterwards.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by