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

6 次查看(过去 30 天)
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-

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-1-10
编辑:Dyuman Joshi 2023-1-11
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Earth, Ocean, and Atmospheric Sciences 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by