Find distance between one point and a subset of other points.

5 ビュー (過去 30 日間)
Samuel L. Polk
Samuel L. Polk 2021 年 2 月 26 日
回答済み: Image Analyst 2021 年 2 月 27 日
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?

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 27 日
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by