Dot products of corresponding matrix slices

5 views (last 30 days)
I have time series of rotation matrices (shape Nx3x3) and frequently need to take dot products of the corresponding matrices in two of such arrays. Can someone come up with a vectorized version of this? I actually have this problem in other situations as well, it's always about trying an "element-wise" operation, where the element is not a single value in an array, like with A .* B, but the element is one slice of a large array. I don't know any efficient way Matlab offers to handle this, here's a for loop version to illustrate what I'm doing. Expanding into matrices of shapes (N*3)x3 doesn't help because it computes all dot product combinations instead of only pairwise.
N = size(a, 1);
result = nan(N, 3, 3);
for i = 1 : N
result(i, :, :) = reshape(a(i, :, :), 3, 3) * reshape(b(i, :, :), 3, 3);
end
Thanks for your help!
  2 Comments
Walter Roberson
Walter Roberson on 15 Apr 2018
That is not a dot product, that is an inner product. If you were doing a dot product then it would just be
sum(A.*B,3)
Julius Krumbiegel
Julius Krumbiegel on 15 Apr 2018
That's true, sorry, I'm talking about matrix multiplication. Sometimes I forget the correct terminology. So I want to do N matrix multiplications with pairs of 3x3 matrices that are stored in two Nx3x3 arrays. The result should be another Nx3x3 array of rotation matrices.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Apr 2018
If you change the array order so that n is the third dimension then there is a file exchange contribution to do the matrix multiplication.
If you have the parallel computing toolbox and you make n the third dimension then imay be able to pagefun() if you have a supported Nvidia gpu.
  1 Comment
Julius Krumbiegel
Julius Krumbiegel on 15 Apr 2018
Thank you, I've found mtimesx on the file exchange after clearing up the dot product / matrix multiplication confusion (I know where it stemmed from now, numpy's dot function does matrix multiplication if the inputs have that shape). This does exactly what I needed.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!