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