2014-10-02 102 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