Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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-

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 10 Jan. 2023
Bearbeitet: Dyuman Joshi am 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 Kommentare
Shin
Shin am 10 Jan. 2023
Hi Dyuman Joshi, thanks for the fast reply, the code works as I wish, thanks a lot, appreciate it .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Earth, Ocean, and Atmospheric Sciences finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by