Matrix multiplication along a specified dimension?

I have a 2x2xN matrix, A, and a 2xN matrix, X. I want to multiply each Nth 2x2 matrix in A by the Nth 2x1 matrix in X. I can do this with:
for i = 1:N
B(:,i) = A(:,:,i)*X(:,i);
end
However, this is very slow, especially in my case where N is large.
Is there a way to do matrix multiplication "along a specified dimension" without a for loop?
Apologies if this is a duplicate question, I looked and could not find anything. Most of the other answers I found involve element-wise multiplication along a specified dimension, which is different than what I am doing.

 採用された回答

Matt J
Matt J 2022 年 9 月 28 日

0 投票

B=pagemtimes(A,X)

2 件のコメント

Darcy Cordell
Darcy Cordell 2022 年 9 月 28 日
Exactly what I was looking for! The speed up is impressive. I would just add that in order for this to work, it is necessary to permute X to be 2x1xN using permute(X,[1,3,2]). Thanks!
James Tursa
James Tursa 2022 年 9 月 28 日
Or reshape(X,2,1,[]);

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

質問済み:

2022 年 9 月 28 日

コメント済み:

2022 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by