Find distance between one point and a subset of other points.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Samuel L. Polk
am 26 Feb. 2021
Beantwortet: Image Analyst
am 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532919/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532924/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532929/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532934/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/532939/image.png)
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 Kommentare
Akzeptierte Antwort
Image Analyst
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!