Finding the nearest neighboors of a point on a 2D plot

35 visualizaciones (últimos 30 días)
Hasan Kaplan
Hasan Kaplan el 30 de Jun. de 2021
Editada: Walter Roberson el 4 de Jul. de 2021
Hello,
I am working on point cloud data and i converted the 3D data to 2D with avoiding Z values. (as plot(X(:,1),Y(:,2),'*')) On this 2D plot i want to find the distances between each point and k-neighboors of it. How can i use the findNearestNeighboors method for this 2D plot?
  2 comentarios
Image Analyst
Image Analyst el 3 de Jul. de 2021
@Yazan, please post this down in the Answers section where you can get credit for it when/if he accepts your answer, rather than up here in the comments which is used to ask for clarification, for example to ask @Hasan Kaplan to attach some actual data in a .mat or .txt file.
Yazan
Yazan el 3 de Jul. de 2021
@Image Analyst Apparently, I added my answer in the comment section by mistake. Thanks for the reminder!

Iniciar sesión para comentar.

Respuesta aceptada

Yazan
Yazan el 3 de Jul. de 2021
Editada: Walter Roberson el 4 de Jul. de 2021
First of all, you have to define your distance. The Euclidean distance, for example?
Step1: Create nearest neighbor searcher object
% use cityblock as a distance, check the documentation for other distance
% measures
% xy is an N-by-2 matrix containing the X-axis and Y-axis values of your data
xy = [xval, yval] % ex: xy = randn(128, 2);
nso = createns(xy, 'Distance', 'cityblock');
Step 2: Find k-nearest neighbors using input data
% k is the number of nearest neighbors
[idx, distk] = knnsearch(nso, xy, 'k', k);
Output:
  • idx is an N-by-k matrix, where each row i is the indices of the k-neareghoest neighbors to xy(i,:)
  • Of course, the first column of idx is the indices of the data itself, as the closest point to a given datapoint is the point itself.
  • distk is an N-by-k matrix, where each row contains the respective k distances

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by