Calculate the distance between a point and all the others
3 views (last 30 days)
Show older comments
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 Comment
Guillaume
on 30 Nov 2016
Edited: Guillaume
on 30 Nov 2016
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.
Accepted Answer
Guillaume
on 30 Nov 2016
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 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!