Find distance between one point and a subset of other points.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Samuel L. Polk
le 26 Fév 2021
Réponse apportée : Image Analyst
le 27 Fév 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 commentaires
Réponse acceptée
Image Analyst
le 27 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!