旋转矩阵按列计算而不是按行计算
我有一个名为“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 at 22 表观 = [cos(theta) -sin(theta) ; sin(theta) cos(theta)] * obj.fixedPositions;
当我更改forest.fixedPositions来存储变量2x250而不是250x2时,上面的代码可以工作,但不会绘图。我将在模拟中不断绘制固定位置,因此我宁愿保留原样,而是让旋转起作用。
有什么想法吗?
另外,固定位置是 xy 点的位置,就像您直视前方一样。即heading = 0。heading设置为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;答:会将其翻转到我需要绘制的内容,但必须有办法避免这种情况?
I have a class called forest and a property called fixedPositions that stores 100 points (x,y) and they are stored 250x2 (rows x columns) in MatLab. When I select 'fixedPositions', I can click scatter and it will plot the points.
Now, I want to rotate the plotted points and I have a rotation matrix that will allow me to do that.
The below code should work:
theta = obj.heading * pi/180;
apparent = [cos(theta) -sin(theta) ; sin(theta) cos(theta)] * obj.fixedPositions;
But it wont. I get this error.
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> landmarks>landmarks.get.apparentPositions at 22
apparent = [cos(theta) -sin(theta) ; sin(theta) cos(theta)] * obj.fixedPositions;
When I alter forest.fixedPositions to store the variables 2x250 instead of 250x2, the above code will work, but it wont plot. I'm going to be plotting fixedPositions constantly in a simulation, so I'd prefer to leave it as it, and make the rotation work instead.
Any ideas?
Also, fixed positions, is the position of the xy points as if you were looking straight ahead. i.e. heading = 0. heading is set to 45, meaning I want to rotate 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
P.S. If you change one line to this: obj.fixedPositions = 100 * rand([2,numberOfTrees]) .* sign(rand([2,numberOfTrees]) - 0.5);
Everything will work fine... it just wont plot.
ans = obj.fixedPositions; ans'; will flip it to what I need to plot, but there has to be a way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种解决方案是计算上述旋转矩阵的转置并将其移动到矩阵乘法的另一侧:
绘制点时,您应该利用 处理图形以创建最流畅的动画。您可以使用绘图对象的句柄和 SET 命令来更新其属性,这应该渲染得更快。以下是使用 SCATTER 函数的示例:
One solution is to compute the transpose of your above rotation matrix and move it to the other side of the matrix multiplication:
When plotting your points, you should take advantage of handle graphics to create the smoothest animation. Instead of erasing the old plot and replotting it, you can use the handle to the plot object and the SET command to update its properties, which should render much faster. Here's an example using the SCATTER function:
我认为你想在乘以旋转之前和之后转置矩阵。如果矩阵是实数,你可以这样做:
I think you want to transpose the matrix before and after multiplying by the rotation. If the matrix is real numbers, you can do: