How to multiply 3D matrices without using for loop

2 ビュー (過去 30 日間)
KostasK
KostasK 2020 年 10 月 16 日
コメント済み: KostasK 2020 年 10 月 16 日
Hi all,
I would like to know if there is a way to multiply 3D matrices, preferably without using a for loop. The reason I do not want to use a for loop is because there is a lot of 'ugly' code involved. Certainly this is not life or death, as there are ways to perform this with a for loop, however it would be good to know if there is an alternative way.
In specific, I have the following code:
A = rand(5, 3, 10) ;
B = rand(5, 10) ;
C = zeros(3, 1, 10) ;
for i = 1:size(A, 3)
C(:,:,i) = A(:,:,i)'*B(:,i) ;
end
D = sum(C, 3) ;

採用された回答

Bruno Luong
Bruno Luong 2020 年 10 月 16 日
編集済み: Bruno Luong 2020 年 10 月 16 日
If you have R2020b
C = pagemtimes(A,'ctranspose',B,'none');
Or using MTIMESX on File-Exchange
I just make a quick comparison and both seem similarly fast.
  1 件のコメント
KostasK
KostasK 2020 年 10 月 16 日
nice, thanks!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by