2-D Meshgrid Rotation Matrix Multiplication

I am trying to rotate the coordinates of a 182x182 mesh grid by means of multiplication with the rotation matrix:
The following code works fine:
[x0,y0] = meshgrid(x,x);
for i = 1:length(theta)
X = x0.*cosd(-i) + -y0.*sind(-i);
Y = x0.*sind(-i) + y0.*cosd(-i);
end
However, I get errors when I try to perform this using the following matrix multiplication:
Here is my code:
[x0,y0] = meshgrid(x,x);
for i = 1:length(theta)
R = [cosd(-i) -sind(-i); sind(-i) cosd(-i)];
[X,Y]'=R.*[x0;y0];
end
And this is the error:
[X,Y]=R.*[x0;y0];
The expression to the left of the equals sign is not a valid target for an assignment.
And when I remove the transpose operator, it says:
Error using .*
Too many output arguments.
What is wrong here? How can I perform this operation using matrix multiplication?
Any guidance is greatly appreciated.

 Réponse acceptée

Stephen23
Stephen23 le 2 Avr 2017
Modifié(e) : Stephen23 le 2 Avr 2017
Firstly you are using the wrong multiplication operator: you need to use matrix times *, not element-wise times .*.
Secondly you are trying to implicitly split the output of this multiplication into parts. But the output of that multiplication is one column vector. Therefore you need to allocate the output to one variable:
Z = R*[x0;y0]
When you write [X,Y] = ... then you are telling MATLAB that the operation has two output variables. But a multiplication does not have two outputs, it only has one.
You have other bugs in your code as well, such as using i as the input to sin and cos, whereas you should use theta(i).

6 commentaires

Sordin
Sordin le 2 Avr 2017
Thank you very much for your response. The meshgrid contains a collection of coordinate pairs. I placed the first coordinates in a matrix x0, and the second coordinates in y0. After the rotation, I need to have each set of coordinates separately (i.e. x coordinates in matrix X, and y-coordinates in a matrix Y). If I allocated the output to one variable, how can I then extract the two new sets of coordinate from the resulting matrix?
X = Z(1);
Y = Z(2);
Thank you. I think this should work. Except I am getting an error on Z = R*[x0;y0]. It says: 'Inner matrix dimensions must agree'. I am very confused because R is just a 2x2 matrix being multiplied by scalars x0 and y0.
This code works:
X = x0.*cosd(-i) + -y0.*sind(-i);
Y = x0.*sind(-i) + y0.*cosd(-i);
But is it not equivalent to the matrix approach I am trying to use?
They're not scalars - they're 2-D arrays. Try
XY = R.*[x0(:),y0(:)];
X = XY(1,:);
Y = XY(2, :);
Thank you for your message. But Matlab still gives the following error when using .* : 'Matrix dimensions must agree'. This was the code:
n = size(Image,1);
x = linspace(-1,1,n);
[x0,y0] = meshgrid(x,x);
for i = 1:length(theta)
R = [cosd(-i) -sind(-i); sind(-i) cosd(-i)];
XY = R.*[x0(:),y0(:)];
X = XY(1,:);
Y = XY(2, :);
end
I wonder if there is any other way to implement this using multiplication with R?
XY = R * [x0(:), y0(:)] .';

Connectez-vous pour commenter.

Plus de réponses (1)

You have
[X,Y]'=R.*[x0;y0];
You need to replace this with something like
XY = R * [x0;y0];
X = XY(1);
Y = XY(2);

3 commentaires

Thank you very much for your input. But when I use this code, I get the following error for the matrix multiplication part:
Inner matrix dimensions must agree.
Is there a way to fix this?
If your R is truly 2 x 2 and your x0 and y0 are scalars, then R * [x0; y0] is correct. Perhaps your R is not 2 x 2 or perhaps your x0 or y0 are not scalars.
I'm very confused because R is definitely a 2x2 matrix:
R = [cosd(-i) -sind(-i); sind(-i) cosd(-i)]
For instance, for θ=1, the output is:
R =
0.9998 0.0175
-0.0175 0.9998
And x0 and y0 should be scalars because they are defined as:
n = size(Image,1);
x = linspace(-1,1,n);
[x0,y0] = meshgrid(x,x)
Why is it that this method doesn't work but the following non-matrix approach does?
X = x0.*cosd(-i) + -y0.*sind(-i);
Y = x0.*sind(-i) + y0.*cosd(-i);
Thank you.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by