2010-05-03 98 views
2

我有一个名为forest的类,一个名为fixedPositions的属性存储100个点(x,y),它们在MatLab中存储为250x2(行x列)。当我选择'fixedPositions'时,我可以点击散点图并绘制点。旋转矩阵按列计算而不是按行计算

现在,我想旋转绘制点,我有一个旋转矩阵,这将允许我这样做。

在下面的代码应工作:

THETA = obj.heading * PI/180; apparent = [cosθ-sinθ; sin(theta)cos(theta)] * obj.fixedPositions;

但它不会。我得到这个错误。

???错误使用==> mtimes 内部矩阵维度必须一致。

错误在==>地标> landmarks.get.apparentPositions at 22 apparent = [cos(theta)-sin(theta); sin(theta)cos(theta)] * obj.fixedPositions;

当我改变forest.fixedPositions来存储变量2x250而不是250x2时,上面的代码将工作,但它不会出现阴谋。我将在模拟中不断绘制fixedPositions,所以我宁愿将它放在原来的位置,然后让旋转工作。

任何想法?

另外,固定位置是xy点的位置,就好像您正在向前看。即标题= 0.标题设置为45,这意味着我想顺时针旋转45度的点。

这里是我的代码:

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 

附:如果您将一行更改为:obj.fixedPositions = 100 * rand([2,numberOfTrees])。* sign(rand([2,numberOfTrees]) - 0.5);

一切都会正常工作......它只是不会情节。

ans = obj.fixedPositions;答;将它转换为我需要绘制的内容,但是必须有一种方法来避免这种情况?

回答

3

我认为你想在旋转之前和之后转置矩阵。如果矩阵是实数,你可以这样做:

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

钱!谢谢。对于像这样的动画来说,最好的方法就是消除散点图并重绘它? – 2010-05-03 23:43:33

4

一种解决方法是计算你上面的旋转矩阵的转置,并将其移动到矩阵乘法的另一面:

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

当绘制你的点数,你应该利用handle graphics来创建最平滑的动画。您可以使用绘图对象的句柄和SET命令来更新其属性,而不是更新旧绘图并重新绘制旧绘图。这里有一个使用SCATTER功能的例子:

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