【问题标题】:Rotation Matrix calculates by column not by row旋转矩阵按列而不是按行计算
【发布时间】:2010-05-03 22:54:49
【问题描述】:

我有一个名为 forest 的类和一个名为 fixedPositions 的属性,它存储 100 个点 (x,y),它们在 MatLab 中存储为 250x2(行 x 列)。当我选择“fixedPositions”时,我可以单击散点图,它会绘制点。

现在,我想旋转绘制的点,我有一个旋转矩阵可以让我这样做。

下面的代码应该可以工作:

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

但它不会。我收到此错误。

???使用 ==> 时出错 内部矩阵尺寸必须一致。

==> 地标>landmarks.get.apparentPositions 在 22 处出错 明显 = [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;回答';会将其翻转为我需要绘制的内容,但必须有办法避免这种情况?

【问题讨论】:

    标签: matlab matrix rotation linear-algebra robotics


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 钱!谢谢。顺便说一句,制作这样的动画的最佳方法是擦除散点图并重新绘制它吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2020-04-19
      • 2012-05-28
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多