필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
Shin
Shin 2023년 1월 10일
편집: Dyuman Joshi 2023년 1월 11일
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
  댓글 수: 2
Shin
Shin 2023년 1월 10일
Hi Dyuman Joshi, thanks for the fast reply, the code works as I wish, thanks a lot, appreciate it .

댓글을 달려면 로그인하십시오.

추가 답변 (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