2017-03-08 154 views
0

我有一組數據點,我想在同一平面上以不同角度繞平面中的每個數據逆時針旋轉一個隨機角度。在第一次嘗試,我可以逆時針飛機大約在同一平面的不同點有一定角度旋轉它們:在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

回答

1

你的問題是,你正在創建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,:)' –

0

你爲什麼不簡單地用imrotate輪換。

例如,要旋轉30度:

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

會按順時針方向旋轉30度,並保持相同的尺寸。要增加大小,你可以在imresize

在旋轉矩陣

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

我想要一個隨機角度爲每個數據。不是一個恆定的角度。我的代碼的第一部分是爲恆定角度編寫的,並且它正常工作 –