フィルターのクリア

Extract values from 3'rd dimmenision with 2D matrix indices

1 回表示 (過去 30 日間)
omri r
omri r 2019 年 10 月 23 日
コメント済み: omri r 2019 年 10 月 23 日
Hi,
Say I have 3D matrix (A), and 2D matrix (B) which each element is the third dimmesnions index I want to extract from A:
A = rand(3,3,3);
B = [1 1 1;
2 2 2;
1 2 3];
So if A is:
A(:,:,1) =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
A(:,:,2) =
0.9649 0.9572 0.1419
0.1576 0.4854 0.4218
0.9706 0.8003 0.9157
A(:,:,3) =
0.7922 0.0357 0.6787
0.9595 0.8491 0.7577
0.6557 0.9340 0.7431
Then the result C would be
C = [0.8147 0.9134 0.2785;
0.1576 0.4854 0.4218;
0.1270 0.8003 0.7431];
How can I find C in matlab?
Would very appreciate any help with that
Thanks in Advence!

採用された回答

Daniel
Daniel 2019 年 10 月 23 日
Hi omri,
hope this helps:
mult = zeros(3,3,3);
mult(:,:,:) = reshape([B==1,B==2,B==3],[3,3,3]);
A_1 = A .* mult;
C = sum(A_1,3)
Cheers,
Daniel
  3 件のコメント
Daniel
Daniel 2019 年 10 月 23 日
In this case I would use a for-loop:
A_1 = zeros(768,1024,72);
for i=1:72
A_1(:,:,i) = A(:,:,i) .* double(B==i);
end
C = sum(A_1,3);
omri r
omri r 2019 年 10 月 23 日
Works like a charm.
Thank you very much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by