Nearest neighbour for one dataset

조회 수: 1 (최근 30일)
AB
AB 2017년 9월 1일
댓글: Walter Roberson 2017년 9월 1일
Hello,
I have one dataset consiting of 3d points. I want to find out within that dataset, which point is closest to which in 3d? Eg; [1 2 3; 3 4 5;6 7 8;8 9 10]... And what are some neat tricks to visualize such results?
Thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2017년 9월 1일
To visualize, you could construct a directed graph using digraph() and plot it with coordinates https://www.mathworks.com/help/matlab/ref/graph.plot.html#buzeikk
  댓글 수: 2
AB
AB 2017년 9월 1일
Hello Walter,
I looked at it. Thanks. I have one question though. The knnsearch works for 2 matrices X and Y. If I have the same dataset as X and Y, will it not count the nearest neighbour for a point in X as the same point in Y. How can I get rid of this?
Thanks!
Walter Roberson
Walter Roberson 2017년 9월 1일
%calculate distances of each point to each other point
dists = squareform( pdist(X) );
%set diagonal to inf so point is not its own closest neighbor
dist(1:size(dists,1)+1:end) = inf;
%find index of closest point
[~, minidx] = min(dists, [], 2);
Now for the K'th point, X(K,:), minidx(K) is the row index of its closest neighbour.
For large number of points, the above might not be as efficient as what knnsearch does, but for modest number of points it is probably more efficient.

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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by