Effacer les filtres
Effacer les filtres

Calculate the distance between a point and all the others

3 vues (au cours des 30 derniers jours)
Marwen Tarhouni
Marwen Tarhouni le 30 Nov 2016
Modifié(e) : Guillaume le 30 Nov 2016
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 commentaire
Guillaume
Guillaume le 30 Nov 2016
Modifié(e) : Guillaume le 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.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 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

Plus de réponses (1)

Torsten
Torsten le 30 Nov 2016
Use "pdist".
Best wishes
Torsten.
  1 commentaire
Guillaume
Guillaume le 30 Nov 2016
Well, the function to use for the distance is the least of the OP problem. Learning matlab would be required first.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by