Rotation matrix is ​​calculated column-wise not row-wise

I have a class called forest and a property called fixedPositions that stores 100 points (x, y) and they are stored in 250x2 (rows x columns) in MatLab. When I select "fixedPositions" I can click the scatter and it will plot points.

Now, I want to rotate the plotted points, and I have a rotation matrix that will allow me to do this.

The following code should work:

theta = obj.heading * pi / 180; obviously = [cos (theta) -sin (theta); sin (theta) cos (theta)] * obj.fixedPositions;

But it won't. I am getting this error.

??? Error when using ==> mtimes The dimensions of the internal matrix must match.

Error in ==> landmarks> landmarks.get.apparentPositions at 22 obviously = [cos (theta) -sin (theta); sin (theta) cos (theta)] * obj.fixedPositions;

When I change forest.fixedPositions to store 2x250 variables instead of 250x2, the above code will work, but it won't depend. I'm going to keep fixing fixed positions in the simulation all the time, so I'd rather leave that as it is and do the rotation instead.

Any ideas?

Also, the fixed positions are the position of the xy points as if you were looking straight ahead. i.e. title = 0. title is set to 45, i.e. I want to rotate the points clockwise 45 degrees.

Here is my code:

classdef landmarks
  properties
    fixedPositions   %# positions in a fixed coordinate system. [x, y]
    heading = 45;     %# direction in which the robot is facing
  end
  properties (Dependent)
    apparentPositions
  end
  methods
    function obj = landmarks(numberOfTrees)
        %# randomly generates numberOfTrees amount of x,y coordinates and set 
        %the array or matrix (not sure which) to fixedPositions
        obj.fixedPositions = 100 * rand([numberOfTrees,2]) .* sign(rand([numberOfTrees,2]) - 0.5);
    end
    function apparent = get.apparentPositions(obj)
        %# rotate obj.positions using obj.facing to generate the output
        theta = obj.heading * pi/180;
        apparent = [cos(theta)  -sin(theta) ; sin(theta)  cos(theta)] * obj.fixedPositions;
    end
  end
end

      

PS If you change one line to this: obj.fixedPositions = 100 * rand ([2, numberOfTrees]). * Sign (rand ([2, numberOfTrees]) - 0.5);

Everything will work well ... it's just not a conspiracy.

ans = obj.fixedPositions; ans; will flip it over to what I need to plot, but there must be a way to avoid this?

+2


a source to share


2 answers


I think you want to transfer the matrix before and after multiplication by rotation. If the matrix is ​​real numbers, you can do:



apparent = ([cos(theta)  -sin(theta) ; sin(theta)  cos(theta)] * (obj.fixedPositions)')';

      

+3


a source


One solution is to calculate the transposition of your aforementioned rotation matrix and transfer it to the other side of the matrix multiplication:

rotMat = [cos(theta) sin(theta); -sin(theta) cos(theta)];  %# Rotation matrix
apparent = (obj.fixedPositions)*rotMat;  %# The result will be a 250-by-2 array

      



When plotting your points, you should use render graphics to create the smoothest animation possible. Instead of erasing the old plot and rewriting it, you can use the plot object descriptor and SET command to update its properties, which should be much faster. Here's an example using the SCATTER function :

h = scatter(apparent(:,1),apparent(:,2));  %# Make a scatter plot and return a
                                           %#   handle to the scattergroup object
%# Recompute new values for apparent
set(h,'XData',apparent(:,1),'YData',apparent(:,2));  %# Update the scattergroup
                                                     %#   object using set
drawnow;  %# Force an update of the figure window

      

+4


a source







All Articles