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

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Daniel on 16 Nov 2011

Hi all. First time posting here, hope I can get some nice ideas. I have 3 large matrices (of equal size), representing the x,y and z coordinates of points. So say my matrices are 200 by 200, it means they are representing 200x200 points. Now, I want to apply this transformation matrix to each point, so that I get some linear combination of the point being transformed. Of course, I know I could opt for a loop where I update the indices of my matrices and each time apply the transformation. But I'd like to know how I can do this more efficiently... I know matlab works better using vectorisation, so I am thinking along the lines of using structures or something... Any ideas?

Thanks. Dan

*No products are associated with this question.*

Answer by Andrei Bobrov on 16 Nov 2011

Accepted answer

**variant**

C = cat(3,x, y, z); s = size(C); Cout = permute(reshape(M*reshape(permute(C,[3 2 1]),s(3),[]),s(3),s(2),[]),[3 2 1]);

**variant 2**

Cout2 = C; for i1 = 1:size(C,2) Cout2(:,i1,:) = (M*squeeze(C(:,i1,:)).').'; end

Answer by Sean de Wolski on 16 Nov 2011

A well written for-loop should be pretty fast. I would recommend doing that. make sure to preallocate your matrix of transformations to be equal to its final size.

Answer by Daniel on 16 Nov 2011

Just FYI... I tested andrei's suggestion with three, 100x100 matrices. I compared its speed with that of the loop method. Andrei's method clocked in at 0.00195sec, whilst the loop method clocked in at 0.0195 ... so andrei's method is 10x faster!! think of larger simulations...this could really be effective :D thanks all again for the suggestions Dan

## 4 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/21386#comment_46460

Can you specify what operation you are trying to do?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/21386#comment_46466

What does "aply this transformation matrix to each point" exactly mean?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/21386#comment_46502

@ Naz & Jan

Sorry I wasn't too clear.

To be exact, the 3 matrices I mention are x,y and z - each with 200 by 200 elements. the (1,1) entry in the matrices represents a certain point say vertex 1, the (1,2) represents vertex 2 etc...

So vertex 1, has coordinates [x(1,1),y(1,1),z(1,1)],

vertex 2, has coordinates [x(1,2),y(1,2),z(1,2)] etc etc...

Now I need to transform the points in these matrices into another coordinate system. I do this transformation using a simple 3x3 matrix (say its called M), which gives me my new coordinates.

So new coordinates of vertex1 = M*(x(1,1),y(1,1),z(1,1)) etc..

But as I said, the 3 matrices contain the different coordinates...

You can look at it anther way..

If you sort of draw a 3Dimensional matrix with the x,y,z insterted into the 3rd dimension, maybe you can visualise better what i mean -

for instance, the new 3D matrix CS will be:

CS(:,:,1)=x;

CS(:,:,1)=y;

CS(:,:,1)=z;

Then each vertex1 would here be CS(1,1,:)...

So in terms of this, the transformation needs to be applied like

M*CS(1,1,:) - but I was asking whether I can do this for all points, rather than cycle through CS using a loop...

did I confuse you more??

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/21386#comment_46503

Sorry I meant to write,

CS(:,:,1)=x;

CS(:,:,2)=y;

CS(:,:,3)=z;