繁体   English   中英

在Matlab中以2d旋转矩阵不同角度

[英]Rotating a matrix by different angles in 2d in matlab

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

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-');

问题是,当我尝试旋转矩阵时,旋转矩阵的尺寸不相等。 在这种情况下,我不知道如何创建旋转矩阵。 有人可以建议如何解决这个问题吗? 任何答案都受到高度赞赏。

您的问题是您正在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.

现在,您需要将每个数据点乘以其相应的旋转矩阵。 乘法仅针对2D矩阵定义,因此我不知道有比遍历每个点更好的方法。

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

你为什么不简单地旋转而不旋转imrotate

例如,您想旋转30度:

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

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

在旋转矩阵中输入随机值

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM