MATLAB Answers


Apply Rotation Matrix to a Set of Points

Asked by AndrewL
on 6 Oct 2012
Accepted Answer by Matt J

Hi all,

I have a set of 3D points (3 by N) to which I am applying a rotation (3 by 3) and translation (1 by 3). I can't figure out a way to get it out of a for-loop, and as you can guess, for loops are too slow.

I currently have:

for i=1:N
   rotPoints(:,i) = rotationMatrix*points(:,i)+translationVector;

giving me what I want. Tried using bsxfun() with it but no success.

Cheers guys, Andrew



1 Answer

Answer by Matt J
on 6 Oct 2012
Edited by Matt J
on 6 Oct 2012
 Accepted answer

Two options:

(1) Loop over rows instead of columns (a much shorter loop)

for i=1:3
   rotPoints(i,:) = rotPoints(i,:)+translationVector(i);

(2) using BSXFUN

   rotPoints=bsxfun(@plus, rotationMatrix*points,  translationVector);

Hard to say which will be faster...


Timed it for my problem, which is for a transform of 640x480 points (307200 operations off the top of my head). 1st method took 0.027217 seconds, 2nd method took 0.025025 seconds.

So 1st is marginally faster in this case. I imagine this could be different for much larger operations though.

Cheers by the way

Matt J
on 6 Oct 2012

marginally slower, I think you mean. Your timings say that the 1st method took longer.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!