Finding closest distance between two data set for each point

조회 수: 4 (최근 30일)
Hi,
I have two data set. Each of them have 300, 3D (x,y,z) data .
Assume;
first data set =m
second data set=n
Every point in "m" corresponds to 1 point in "n" which have to be the closest one.
I need to find the closest "n" to "m" and calculate the distance between them and i need to do it for all 300 data
I am new with matlab. I think i should use pdist2 but i could not find how.
Thank you...

채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 29일
d = pdist2(first_xyz, second_xyz);
[mindist, idx] = min(d, [], 2);
Here first_xyz and second_xyz should be something-by-3 arrays of x, y, z coordinates.
The idx that results will have as many rows as first_xyz has, and the value will be the index of which second_xyz row is closest to that point; the mindist value will tell you how close it is (no more computation will be required.)
If you want the index for each n (second dataset) as to which m it is closest to, then use min(d, [], 1)

추가 답변 (1개)

Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019년 3월 29일
Thank you for your answer,
Actually i can calculate the min. distance but i want to get the results like given below;
(C1(x,y,z), C2 (x,y,z), distance between C1 and C2)
*C2 (x,y,z) must be the closest point to C1
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 3월 29일
편집: Walter Roberson 2019년 3월 29일
Assuming that C1 and C2 have 3 columns, giving X Y Z coordinates
d = pdist2(C1, C2);
[mindist, idx] = min(d, [], 2);
for K = 1 : length(idx)
fprintf('C1(%d,:)@(%f,%f,%f) <-> C2(%d,:)@(%f,%f,%f) is distance %f\n', K, C1(K,:), idx(K), C2(idx(K),:), mindist(K));
end
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019년 3월 29일
Thank you very much, this is certainly what i need.

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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by