Find distance between one point and a subset of other points.
6 views (last 30 days)
Show older comments
Samuel L. Polk
on 26 Feb 2021
Answered: Image Analyst
on 27 Feb 2021
I would like to find the distance between a point and all points in a matrix X, where : a subset of . Mathematically, this would correspond to a vector with entries equal to the elements of the following set:
,
One way to do this is the following:
[~,Dist_i] = knnsearch(X(i,:),X, 'K', n);
Dist_iJ = Dist_i(J);
However, this requires n nearest neighbor searches, so the above is not likely to scale well. Is there a fast, ideally built-in way to do this task?
0 Comments
Accepted Answer
Image Analyst
on 27 Feb 2021
For example, with 2-D array X, and 4 points:
n = 4; % Whatever
X = rand(n, 2);
% Find distance between point 1 and all other points
allDistances = pdist2(X(1, :), X)
% Find distance between point 2 and all other points
allDistances = pdist2(X(2, :), X)
% Find distance between point 3 and all other points
allDistances = pdist2(X(3, :), X)
% And so on.
you get
allDistances =
0 0.137705462706709 0.300486552306604 0.770204210452277
allDistances =
0.137705462706709 0 0.176471014289295 0.648765114726253
allDistances =
0.300486552306604 0.176471014289295 0 0.596340359329654
Adapt as needed for other n.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!