在matlab中通过2d中的不同角度旋转矩阵

问题描述:

我有一组数据点,我想在同一平面上以不同角度绕平面中的每个数据逆时针旋转一个随机角度。在第一次尝试,我可以逆时针飞机大约在同一平面的不同点有一定角度旋转它们:在matlab中通过2d中的不同角度旋转矩阵

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

的问题是,当我尝试旋转矩阵,旋转矩阵尺寸不等于所应当。我不知道如何在这种情况下创建旋转矩阵。 任何人都可以建议如何解决这个问题?任何答案都非常感谢。

+0

有没有人可以回答这个问题有点奇怪 –

+0

这不是“奇怪”。要么现在没有人可以回答你,或者人们根本不知道答案。我们在这里回答关于志愿者能力的问题。这不是全职工作。立即期待答案不是你应该在这里采取的行为。 – rayryeng

你的问题是,你正在创建R的20X2矩阵要知道为什么,考虑

使用 'full'选项,输入一个随机值
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 
+0

它是不正确的。 –

+0

请问您可以扩充一下您的意思 –

+0

vv = [1,2,1; 1,1,-1];披= [0; 0; 0];中心= [0,0,0; ​​0,0,0]; R1 = [cos(phi)-sin(φ)]; %创建一个10x2矩阵 R2 = [sin(phi)cos(phi)]; %创建一个10x2矩阵 R = cat(3,R1,R2)%Cocatenate(“粘贴”)两个矩阵沿着三维创建一个10x2x2矩阵 R =置换(R,[2,3,1]); vx = voo(1,:)'vy = voo(2,:)' –

你为什么不简单地用imrotate轮换。

例如,要旋转30度:

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

会按顺时针方向旋转30度,并保持相同的尺寸。要增加大小,你可以在imresize

在旋转矩阵

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

我想要一个随机角度为每个数据。不是一个恒定的角度。我的代码的第一部分是为恒定角度编写的,并且它正常工作 –