Multiply two 3D arrays that result would be 4D

1 回表示 (過去 30 日間)
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 13 日
コメント済み: Mantas Vaitonis 2018 年 7 月 13 日
Hello to All,
I am trying to work and understand 4D arrays. Lets say I have two 3D gpuArrays a(100x5x5) and b(5x5x5). I would like to multiply them that it would result in 4D array c(100x5x5x5), and example of multiplication:
For j=1:5
For i=1:5
c(:,:,i,j)=a(:,i,j)*b(i,:,j)
end
end
What would be the way to achieve this. I am trying to go for 4D arrays, because I would like to avoid for loops.
  2 件のコメント
James Tursa
James Tursa 2018 年 7 月 13 日
a(:,i,j) is a 100x1. b(i,:,j) is a 1x5. So the result of a matrix multiply is going to be a 100x5. This would mean you need c(:,:,i,j) instead of c(:,i,j) on the lhs. Is this what you want to do?
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 13 日
@James Tursa, yes you are correct, c suppose to be c(:,:,i,j), I fixed my comment. Thank You for noticing.

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

採用された回答

James Tursa
James Tursa 2018 年 7 月 13 日
Assuming I understand what you are trying to do,
result = permute(a,[1 4 2 3]) .* permute(b,[4 2 1 3]);
or in earlier versions of MATLAB
result = bsxfun(@times,permute(a,[1 4 2 3]) , permute(b,[4 2 1 3]));
  1 件のコメント
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 13 日
Yes this is exactly what I need. Thank You and you guys always give such great and fast responses. Now I can move forward.

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

その他の回答 (1 件)

Matt J
Matt J 2018 年 7 月 13 日
c=a.*permute(b,[4,1,3,2]);

カテゴリ

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