2010-05-03 79 views
2

我有一個名爲forest的類,一個名爲fixedPositions的屬性存儲100個點(x,y),它們在MatLab中存儲爲250x2(行x列)。當我選擇'fixedPositions'時,我可以點擊散點圖並繪製點。旋轉矩陣按列計算而不是按行計算

現在,我想旋轉繪製點,我有一個旋轉矩陣,這將允許我這樣做。

在下面的代碼應工作:

THETA = obj.heading * PI/180; apparent = [cosθ-sinθ; sin(theta)cos(theta)] * obj.fixedPositions;

但它不會。我得到這個錯誤。

???錯誤使用==> mtimes 內部矩陣維度必須一致。

錯誤在==>地標> landmarks.get.apparentPositions at 22 apparent = [cos(theta)-sin(theta); sin(theta)cos(theta)] * obj.fixedPositions;

當我改變forest.fixedPositions來存儲變量2x250而不是250x2時,上面的代碼將工作,但它不會出現陰謀。我將在模擬中不斷繪製fixedPositions,所以我寧願將它放在原來的位置,然後讓旋轉工作。

任何想法?

另外,固定位置是xy點的位置,就好像您正在向前看。即標題= 0.標題設置爲45,這意味着我想順時針旋轉45度的點。

這裏是我的代碼:

classdef landmarks 
    properties 
    fixedPositions %# positions in a fixed coordinate system. [x, y] 
    heading = 45;  %# direction in which the robot is facing 
    end 
    properties (Dependent) 
    apparentPositions 
    end 
    methods 
    function obj = landmarks(numberOfTrees) 
     %# randomly generates numberOfTrees amount of x,y coordinates and set 
     %the array or matrix (not sure which) to fixedPositions 
     obj.fixedPositions = 100 * rand([numberOfTrees,2]) .* sign(rand([numberOfTrees,2]) - 0.5); 
    end 
    function apparent = get.apparentPositions(obj) 
     %# rotate obj.positions using obj.facing to generate the output 
     theta = obj.heading * pi/180; 
     apparent = [cos(theta) -sin(theta) ; sin(theta) cos(theta)] * obj.fixedPositions; 
    end 
    end 
end 

附:如果您將一行更改爲:obj.fixedPositions = 100 * rand([2,numberOfTrees])。* sign(rand([2,numberOfTrees]) - 0.5);

一切都會正常工作......它只是不會情節。

ans = obj.fixedPositions;答;將它轉換爲我需要繪製的內容,但是必須有一種方法來避免這種情況?

回答

3

我認爲你想在旋轉之前和之後轉置矩陣。如果矩陣是實數,你可以這樣做:

apparent = ([cos(theta) -sin(theta) ; sin(theta) cos(theta)] * (obj.fixedPositions)')'; 
+0

錢!謝謝。對於像這樣的動畫來說,最好的方法就是消除散點圖並重繪它? – 2010-05-03 23:43:33

4

一種解決方法是計算你上面的旋轉矩陣的轉置,並將其移動到矩陣乘法的另一面:

rotMat = [cos(theta) sin(theta); -sin(theta) cos(theta)]; %# Rotation matrix 
apparent = (obj.fixedPositions)*rotMat; %# The result will be a 250-by-2 array 

當繪製你的點數,你應該利用handle graphics來創建最平滑的動畫。您可以使用繪圖對象的句柄和SET命令來更新其屬性,而不是更新舊繪圖並重新繪製舊繪圖。這裏有一個使用SCATTER功能的例子:

h = scatter(apparent(:,1),apparent(:,2)); %# Make a scatter plot and return a 
              %# handle to the scattergroup object 
%# Recompute new values for apparent 
set(h,'XData',apparent(:,1),'YData',apparent(:,2)); %# Update the scattergroup 
                %# object using set 
drawnow; %# Force an update of the figure window