Nearest neighbour distances for each part of dataset

2 ビュー (過去 30 日間)
Jorg Borren
Jorg Borren 2022 年 5 月 17 日
回答済み: Nithin 2025 年 3 月 27 日
I have a dataset consisting of 3 parts (uploaded picture) interrupted by random integers. The left and right column correspond to coordinates x and y respectively of the points.
How can I find the nearest neighbour of each point, and their euclidean distances, but only compared to the points of the same part of the dataset (e.g. nearest neighbor in part 1 only come from part 1, part 2 consists of only one point, so the point won't have any nearest neighbours).

回答 (1 件)

Nithin
Nithin 2025 年 3 月 27 日
To find the nearest neighbor for each point within the same part and calculate their Euclidean distances, you can follow the below steps:
  • First, parse the dataset to separate the points into distinct parts based on the leading integers. For each part, calculate the Euclidean distance between each point and every other point in that part. This involves iterating over the points and using the Euclidean distance formula to measure the distance between pairs. Kindly refer to the following documentation to know more about handling text files: https://www.mathworks.com/help/matlab/text-files.html
  • Identify the nearest neighbor for each point by finding the point with the smallest distance that isn't itself. If a part has fewer than two points, skip the nearest neighbor calculation for that part since it is not applicable.
  • Finally, output the nearest neighbors and their corresponding distances for each point in each part.
Please find the attached sample code to understand the implementation better.

Community Treasure Hunt

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

Start Hunting!

Translated by