在matlab中以不同角度旋转二维矩阵
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-');
问题是,当我尝试旋转矩阵时,旋转矩阵维度不应该相等。我不知道在这种情况下应该如何创建旋转矩阵。
谁能建议如何解决这个问题?非常感谢任何答案。
你为什么不简单地用 imrotate
旋转。
比如你想旋转30度:
newmat = imrotate(mat, 30, 'crop')
将顺时针旋转30度并保持尺寸不变。要增加大小,您可以使用 imresize
中的 'full'
选项
在旋转矩阵中输入一个随机值
rn = rand*90; %0-90 degrees
newmat = imrotate(mat, rn, 'crop')
您的问题是您在 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
我有一组数据点,我想将每个数据点在平面上逆时针旋转一个随机角度关于同一平面上的不同点。第一次尝试,我可以将它们在平面中逆时针旋转一定角度关于同一平面中的不同点:
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-');
问题是,当我尝试旋转矩阵时,旋转矩阵维度不应该相等。我不知道在这种情况下应该如何创建旋转矩阵。 谁能建议如何解决这个问题?非常感谢任何答案。
你为什么不简单地用 imrotate
旋转。
比如你想旋转30度:
newmat = imrotate(mat, 30, 'crop')
将顺时针旋转30度并保持尺寸不变。要增加大小,您可以使用 imresize
'full'
选项
在旋转矩阵中输入一个随机值
rn = rand*90; %0-90 degrees
newmat = imrotate(mat, rn, 'crop')
您的问题是您在 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