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

Thread Subject:
Quicker way to rotate a 3 dimensional array?

Subject: Quicker way to rotate a 3 dimensional array?

From: Phil

Date: 28 Jan, 2013 04:07:07

Message: 1 of 4

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?

Subject: Quicker way to rotate a 3 dimensional array?

From: Roger Stafford

Date: 28 Jan, 2013 04:48:06

Message: 2 of 4

"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

Subject: Quicker way to rotate a 3 dimensional array?

From: Bruno Luong

Date: 28 Jan, 2013 07:31:08

Message: 3 of 4

"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

R = [ 1,0,0; 0,cos(theta),sin(theta); 0,-sin(theta),cos(theta)]

[nx ny nz] = size(temp);
temp3 = reshape(reshape(temp,[],3)*R, [nx ny 3]);

% Bruno

Subject: Quicker way to rotate a 3 dimensional array?

From: Phil

Date: 28 Jan, 2013 13:48:07

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ke59fr$8uo$1@newscl01ah.mathworks.com>...
> "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
>
> R = [ 1,0,0; 0,cos(theta),sin(theta); 0,-sin(theta),cos(theta)]
>
> [nx ny nz] = size(temp);
> temp3 = reshape(reshape(temp,[],3)*R, [nx ny 3]);
>
> % Bruno

Perfect, I dropped this in and it worked right away and sped up the code considerably. I couldn't get Roger's solution to work unfortunately.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us