distance of points to points calculation

1 回表示 (過去 30 日間)
Yu Li
Yu Li 2018 年 7 月 11 日
回答済み: Anton Semechko 2018 年 7 月 11 日
Hi: I have two set of 2D scatter points, says A and B, I need the minimun value of each points in A to B. for example, the size of A and B are 100*2, and below are my codes:
for i=1:1:100
ppdist=((A(j,1)-B(:,1)).^2+(A(j,2)-B(:,2)).^2).^0.5;
result(i)=min(ppdist);
end
however, if the size of A and B are significantly large, for example: 1 million for each, the calculation cost significantly large amount of time.
so I want know if there is anyway to optimize this calculation?
Thanks Yu

採用された回答

Anton Semechko
Anton Semechko 2018 年 7 月 11 日
Use 'knnsearch' function. For example:
[idxA,D_a2b]=knnsearch(B,A,'k',1);
will return indices of the points in set A closest to B, so that A(idxA(j),:) is the closest point to B(j,:). D_a2b(j) is the distance between these closest point pairs.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by