【问题标题】:Rotating a matrix by different angles in 2d in matlab在matlab中以2d的不同角度旋转矩阵
【发布时间】:2017-03-08 05:21:43
【问题描述】:

我有一组数据点,我想将每个数据在平面中逆时针旋转一个随机角度,围绕同一平面中的不同点。在第一次尝试中,我可以将它们在同一平面上的不同点逆时针旋转一定角度:

x = 16:25;
y = 31:40;
% create a matrix of these points, which will be useful in future  calculations
v = [x;y];
center = [6:15;1:10];
% define a 60 degree counter-clockwise rotation matrix
theta = pi/3;       % pi/3 radians = 60 degrees
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
% do the rotation...
vo = R*(v - center) + center;
% pick out the vectors of rotated x- and y-data
x_rotated = vo(1,:);
y_rotated = vo(2,:);
% make a plot
plot(x, y, 'k-', x_rotated, y_rotated, 'r-');

然后我尝试将其概括为随机天使旋转,但有一个问题我无法在第二个代码中解决:

x = 16:25;
y = 31:40;
% create a matrix of these points, which will be useful in future   calculations
v = [x;y];
center = [6:15;1:10]; %center of rotation
% define random degree counter-clockwise rotation matrix
theta = pi/3*(rand(10,1)-0.5);       % prandom angle 
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
% do the rotation...
 vo = R*(v - center) + center;
% pick out the vectors of rotated x- and y-data
x_rotated = vo(1,:);
y_rotated = vo(2,:);
% make a plot
plot(x, y, 'k-', x_rotated, y_rotated, 'r-');

问题是,当我尝试旋转矩阵时,旋转矩阵的维度并不像它应该的那样相等。我不知道在这种情况下我应该如何创建旋转矩阵。 谁能建议如何解决这个问题?任何答案都非常感谢。

【问题讨论】:

  • 这个问题没人能回答,有点奇怪
  • 这并不“奇怪”。要么现在没有人可以回答你,要么人们根本不知道答案。我们在这里回答有关志愿者身份的问题。这不是一份全职工作。期望立即得到答案不是您应该采取的行为。

标签: matlab matrix rotation matlab-guide


【解决方案1】:

您的问题是您正在 R 中创建一个 20x2 矩阵。要了解原因,请考虑

theta % is a 10x1 vector

cos(theta)  % is also going to be a 10x1 vector

[cos(theta) -sin(theta);...
 sin(theta) cos(theta)];  % is going to be a 2x2 matrix of 10x1 vectors, or a 20x2 matrix

您想要的是访问每个 2x2 旋转矩阵。一种方法是

R1 = [cos(theta) -sin(theta)] % Create a 10x2 matrix
R2 = [sin(theta) cos(theta)] % Create a 10x2 matrix

R = cat(3,R1,R2) % Cocatenate ("paste") both matrix along the 3 dimension creating a 10x2x2 matrix

R = permute(R,[3,2,1]) % Shift dimensions so the matrix shape is 2x2x10, this will be helpful in the next step.

现在您需要将每个数据点乘以其对应的旋转矩阵。乘法只为二维矩阵定义,所以我不知道比循环遍历每个点更好的方法。

for i = 1:length(v)
    vo(:,i) = R(:,:,i)*(v(:,i) - center(:,i)) + center(:,i);
end

【讨论】:

  • 能否请您稍微扩展一下您的意思
  • vv=[1,2,1;1,1,-1]; phi=[0;0;0];中心=[0,0,0;0,0,0]; R1 = [cos(phi) -sin(phi)]; % 创建一个 10x2 矩阵 R2 = [sin(phi) cos(phi)]; % 创建一个 10x2 矩阵 R = cat(3,R1,R2) % 沿 3 维连接(“粘贴”)两个矩阵,创建一个 10x2x2 矩阵 R = permute(R,[2,3,1]) ; vx = voo(1,:)' vy = voo(2,:)'
  • 以上评论是 3 个向量的示例。通过旋转 angel 等于 0。旋转后我们不希望向量改变。但它们会改变并变成等于一
  • 请运行评论并查看结果
  • 请您再检查一次,因为它在我这边工作正常。请确认每个旋转矩阵R(:,:,i)是一个单位矩阵
【解决方案2】:

你为什么不简单地用imrotate旋转。

比如你想旋转30度:

newmat = imrotate(mat, 30, 'crop')

将顺时针旋转 30 度并保持尺寸不变。要增加大小,您可以在imresize 中使用'full' 选项

在旋转矩阵中输入一个随机值

rn = rand*90; %0-90 degrees
newmat = imrotate(mat, rn, 'crop')

【讨论】:

  • 我想要每个数据的随机角度。不是一个恒定的角度。我的代码的第一部分是为恒定角度编写的,它可以正常工作
  • this linkimrotate 逆时针旋转,顺时针旋转角度应该是负数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-17
相关资源
最近更新 更多