Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Quicker way to rotate a 3 dimensional array?
Replies: 2   Last Post: Jan 28, 2013 2:31 AM

 Messages: [ Previous | Next ]
 Roger Stafford Posts: 5,929 Registered: 12/7/04
Re: Quicker way to rotate a 3 dimensional array?
Posted: Jan 27, 2013 11:48 PM

"Phil " <matlab@test.com> wrote in message <ke4thb\$s7j\$1@newscl01ah.mathworks.com>...
> I've got an array that is three dimensional where the first two dimensions are relatively large numbers and the third has length 3. This third one contains x,y,z co-ordinates that I would like to rotate about the x axis. I've managed to do this using:
>
> tmp3 = tmp;
> k = 0;
> for i=1:height
> for j=1:width
> k = k + 1;
> tmp3(i,j,:) = reshape(tmp(i,j,:),1,3)*[1,0,0;0,cos(theta),sin(theta);0,-sin(theta),cos(theta)];
> end
> end
>
> This works but as it's iterating, it's quite slow. Is there a faster way to do this?

- - - - - - - - - -
Assuming theta is a scalar try this:

tmp3 = tmp;
tmp3(:,:,2) = tmp(:,:,2)*cos(theta)-tmp(:,:,3)*sin(theta);
tmp3(:,:,3) = tmp(:,:,2)*sin(theta)-tmp(:,:,3)*cos(theta);

Roger Stafford

Date Subject Author
1/27/13 Roger Stafford
1/28/13 Bruno Luong