Calculate the distance between a point and all the others
3 次查看(过去 30 天)
显示 更早的评论
Hi, I try to calculate the distance between a point and all the others. for exemple A(-20,90) B(-24,50) C(-23,90) D(-13,50) E(50,16)
I want to calculate the distance between (A and B) & (A and C) &(A and D) & (A and E)
also (B and A) & (B and C) &(B and D) & (B and E)
do you have an idea ?
x1= -20;
y1=90;
x2=-24;
y2=50;
x3=-23;
y3=90;
x4=-13;
y4=59;
x5= 50;
y6=16;
i=5;
% for while
while i<=4
i=i-1;
distance = sqrt((xi-x1)^2+(yi-y1)^2); ------------------> error distance???
end
Thanks
1 个评论
Guillaume
2016-11-30
编辑:Guillaume
2016-11-30
Where have you seen that matlab would magically understand that the i in the variable name xi is supposed to be substituted by the actual value of the variable i in order to make a new variable name?
There is no point in telling you how to calculate the distance if you don't even know how to index in matlab. Please, go through the getting started tutorial.
采纳的回答
Guillaume
2016-11-30
See comment to question. You need to learn matlab first.
In R2016b, this is very straightforward:
points = [-20, 90;
-24, 50;
-23, 90;
-13, 50;
50, 16];
distance = hypot(points(:, 1) - points(1, 1), points(:, 2) - points(1, 2)) %distances to 1st point
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!