Effacer les filtres
Effacer les filtres

How to calculate distance between a set of point to multiple point

6 vues (au cours des 30 derniers jours)
Shin
Shin le 10 Jan 2023
Modifié(e) : Dyuman Joshi le 11 Jan 2023
Hi there, I have two sets of coordinates,
setA = [2,1; 2,3; 4,6];
setB = [8,8; 9,8; 3,7; 5,8; 9,2; 3,4; 5,7];
and I wanted to calculate the distance between the two sets, for example, the first coordinate of "setA" [2,1] to all the coordinates in "setB" and the second coordinate in "setA" [2,3] also with the all in "setB", and so on..., and at the end, i'll have 7 sets of distance calculated in each of the "setA", how can I do it? Thanks.
-Chann-

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 10 Jan 2023
Modifié(e) : Dyuman Joshi le 11 Jan 2023
setA = [2,1; 2,3; 4,6];
setB = [8,8; 9,8; 3,7; 5,8; 9,2; 3,4; 5,7];
%single element
dist=vecnorm(setB-setA(1,:),2,2)
dist = 7×1
9.2195 9.8995 6.0828 7.6158 7.0711 3.1623 6.7082
%for all the points in setA
s=size(setA,1);
%pre-allocation
y=cell(1,s);
for idx=1:s
y{1,idx}=vecnorm(setB-setA(idx,:),2,2);
end
y
y = 1×3 cell array
{7×1 double} {7×1 double} {7×1 double}
%you can compare the values to the above result for reference
y{1}
ans = 7×1
9.2195 9.8995 6.0828 7.6158 7.0711 3.1623 6.7082
Here, y{1} will correspond to distances of 1st point of setA to all points of setB
y{2} for 2nd point of setA to all points of setB
and y{3} for 3rd point of setA to all points of setB
  2 commentaires
Shin
Shin le 10 Jan 2023
Hi Dyuman Joshi, thanks for the fast reply, the code works as I wish, thanks a lot, appreciate it .
Dyuman Joshi
Dyuman Joshi le 10 Jan 2023
You are welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Earth, Ocean, and Atmospheric Sciences dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by