登录论坛

查看完整版本 : 旋转矩阵按列而不是按行计算


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

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

下面的代码应该工作:

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

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

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

错误==>地标> 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 PS:如果您将其中一行更改为:obj.fixedPositions = 100 * rand([2,numberOfTrees])。* sign(rand([2,numberOfTrees])-0.5);

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

ans = obj.fixedPositions; ans';将其翻转到我需要绘制的位置,但是必须有一种避免这种情况的方法?



回答:

我想您想在乘以旋转前后对矩阵进行转置。如果矩阵是实数,则可以执行以下操作:

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

更多&回答... (https://stackoverflow.com/questions/2761748)