Applying array of transform matrices to array of vectors

I have a large array of 3x3 transform matrices, e.g. 3x3x700 that I would like to apply to a corresponding array of 3x1 vectors e.g. 3x1x700 to obtain an array of transformed vectors. Is there a way of vectorising this operation that is more efficient than using a for loop?
Thanks,
Steve

 Accepted Answer

Check out pagemtimes. Whether or not it's faster than a loop ... would need to be tested on your system
D = rand(3,3,700);
v = rand(3,1,700);
tic,y = pagemtimes(D,v);toc
Elapsed time is 0.005295 seconds.
z = 0*y;
tic
for ii = 1:700
z(:,1,ii) = D(:,:,ii)*v(:,1,ii);
end
toc
Elapsed time is 0.006762 seconds.
isequal(z,y)
ans = logical
1

1 Comment

That's great, thank you. On my system your code takes 327 us for pagemtimes compared with 2435 us for the for loop.

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!