Clear Filters
Clear Filters

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

6 views (last 30 days)
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-

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 10 Jan 2023
Edited: Dyuman Joshi on 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

More Answers (0)

Categories

Find more on Earth, Ocean, and Atmospheric Sciences in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!