Clear Filters
Clear Filters

Matrix multiplication along a specified dimension?

25 views (last 30 days)
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);
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.

Accepted Answer

Matt J
Matt J on 28 Sep 2022
Darcy Cordell
Darcy Cordell on 28 Sep 2022
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!

Sign in to comment.

More Answers (0)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!