Finding the nearest neighboors of a point on a 2D plot
34 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2021-7-3
@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
2021-7-3
@Image Analyst Apparently, I added my answer in the comment section by mistake. Thanks for the reminder!
采纳的回答
Yazan
2021-7-3
编辑:Walter Roberson
2021-7-4
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
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!