I have this code here and it does what I want it to do just fine. however theres a lot of repetition. I was wondering if there was a way to make it better, maybe even remove the for loop?
A = randi(10,5,5,5);
B = randi(4,5,5);
C = zeros(4,1,5);
layer1 = A(:,:,1); %separate 3rd dimension
layer2 = A(:,:,2);
layer3 = A(:,:,3);
layer4 = A(:,:,4);
layer5 = A(:,:,5);
for i = 1:4
value = B == i; %create a logical index for values from 1-4 in B
C(i,:,1) = mean(layer1(value));
C(i,:,2) = mean(layer2(value));
C(i,:,3) = mean(layer3(value));
C(i,:,4) = mean(layer4(value));
C(i,:,5) = mean(layer5(value));
end

2 件のコメント

Amy Chen
Amy Chen 2018 年 8 月 31 日
how?
Adam
Adam 2018 年 8 月 31 日
Even without vectorisation, from the perspective of purely how much code you write you should never be creating 5 variables as 5 layers of another array. The 5 layers you want are all already there in A, just index into them.
Then the indexing would be obvious to be able to create a nested for loop (from 1 to 5, indexing into A(:,:,1-5) rather than 5 named variables).
When you have the simpler nested for loop it is then often a lot easier to see how to vectorise the code. Or you could try to jump straight to the vectorisation. The intermediate step often just helps with understanding the process.

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

 採用された回答

Stephen23
Stephen23 2018 年 8 月 31 日
編集済み: Stephen23 2018 年 8 月 31 日

0 投票

One easy solution based on accumarray:
S = size(A);
[~,Y,Z] = ndgrid(1:S(1),1:S(2),1:S(3));
Y(:) = 1;
X = [repmat(B(:),S(3),1),Y(:),Z(:)];
C2 = accumarray(X,A(:),[],@mean)
And tested:
>> isequal(C2,C)
ans = 1

3 件のコメント

Amy Chen
Amy Chen 2018 年 8 月 31 日
could you please explain what each line is doing? thanks ^_^
Stephen23
Stephen23 2018 年 8 月 31 日
編集済み: Stephen23 2018 年 8 月 31 日
@Amy Chen: I used accumarray to apply the function mean to the elements of A, following the indexing given in X. X contains the indices for the output array, where the first column is for dimension one, the second column for dimension two, etc., of the output array. Take a closer look at the first few rows:
>> X(1:7,:)
ans =
5 1 1
3 1 1
2 1 1
4 1 1
2 1 1
4 1 1
3 1 1
The first row tells accumarray to take the first value of the second input (which is A(:)) and join it into output array's element (5,1,1), the second value into the output's element (3,1,1), the third value into the output's element (2,1,1), etc. Before those elements are actually put into the output, mean is applied to each of those collections of values.
So most of my answer just generates suitable indices, nothing more. The trick is to realize that B defines the row indices for each page. The actual calculation is performed by accumarray and mean, and because of the indices specified in X it also returns data in the exact required shaped array. A break down:
S = size(A); % size of the input matrix.
[~,Y,Z] = ndgrid(1:S(1),1:S(2),1:S(3)); % column and page indices.
Y(:) = 1; % set column indices to 1 (your output only has one column).
X = [repmat(B(:),S(3),1),Y(:),Z(:)]; % join row, column, page indices.
C2 = accumarray(X,A(:),[],@mean) % calculate mean, put into output.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 31 日
Great @Stephen Cobeldick

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

質問済み:

2018 年 8 月 31 日

編集済み:

2018 年 8 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by