2014-10-02 101 views
4

我目前遇到題目中提到的問題。 如果我只是想有3個獨立的點,並錄製成與MATLAB 3個座標點是很容易像下面在點列表中存儲三維座標Matlab

A=[0 0 1];%coordinates of Q1 
B=[0 0 -1];%coordinates of Q2 
C=[0 1 0];%coordinates of Q3 

因此,描述A(0,0,1),B(座標0,0,-1),C(0,1,0)。在進一步的計算,我可以使用的座標,並做了計算,如:

R1=A-B; %vector from Q2 to Q1 
R2=A-C; %vector from Q3 to Q1 
R3=C-B; %vector from Q2 to Q3 

但是,如果我想生成多點像隨機100分,我在上面寫着的方式是愚蠢的。而且我也想像以前一樣使用座標,因爲它比較方便。以下是我嘗試過的方法。

% random distributed points 
x = rand(Number_of_Points, 1); 
y = rand(Number_of_Points, 1); 
z = x.^2 + y.^2; 

Points = [x(:) y(:) z(:)]; 

但它只是記錄所有點的3個座標,我不能單獨記錄它們,因爲我之前做過。我想通過使用Points(1)-Points(2)來計算矢量嗎?有沒有人有想法該怎麼做?

+2

如果你想要點之間的距離,你應該看看'pdist' – Dan 2014-10-02 09:06:55

回答

2

你只需要使用的,而不是線性索引下標索引:

Points(1,:) - Points(2,:) 

否則,如果你想歐幾里得距離,你可以不喜歡它

sqrt(sum((Points(1,:) - Points(2,:)).^2)) 

或讓自己的匿名函數:

PointsDistance = @(a,b)(sqrt(sum((Points(a,:) - Points(b,:)).^2))) 

現在你可以撥打

PointsDistance(1,2) 
+0

謝謝,@丹這工作 – 2014-10-02 09:12:32