# find the minimum distances of all points from neighboring points

4 views (last 30 days)
HJ on 4 Jun 2021
Answered: darova on 5 Jun 2021
When there are a lot of points, I want to find the minimum distances of all points from neighboring points.
It's too slow right now because there are so many dots.
distances = [];
for i = 1:k
for j = 1:k
if i==j
distances(i,j) = 100000;
else
distances(i,j) = pdist2(blobMeasurements(i).Centroid, blobMeasurements(j).Centroid);
end
end
end
[distances_min, distances_min_index] = min(distances);
I need to find the distance from a point to the nearest point
Also, I have to calculate this process for every point.
Is there a good way?

darova on 5 Jun 2021
Program is slow because of pre-allocation and calling pdist2 everytime
try this
D = pdist2(centroid,centroid);
D = D + eye(size(D))*1E5;

### More Answers (1)

Sulaymon Eshkabilov on 4 Jun 2021
Here is a nice discussion on this issue with some well developed scripts:
##### 1 CommentShowHide None
HJ on 4 Jun 2021
It's a good resource, but the content is different.
I need to find the distance from a point to the nearest point
Also, I have to calculate this process for every point.