MATLAB Answers

AndrewL

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;
end

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

Cheers guys, Andrew

  0 Comments

Products

1 Answer

Matt J
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)

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

(2) using BSXFUN

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

Hard to say which will be faster...

  2 Comments

AndrewL
on 6 Oct 2012

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.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today