MATLAB Answers

Given a matrix of n rows and 2 columns containing the xOy coordinates of n points. Find two points with distance and return the row index of those two points

1 ビュー (過去 30 日間)
Phung Khai
Phung Khai 2021 年 9 月 7 日
コメント済み: Phung Khai 2021 年 9 月 10 日
can you help me : Given a matrix of n rows and 2 columns containing the xOy coordinates of n points. Find two points with distance and return the row index of those two points

回答 (1 件)

DGM
DGM 2021 年 9 月 7 日
Consider:
n = 10; % number of points
d = 7; % distance to match
P = 10*rand(n,2); % example point list
D = sqrt((P(:,1)-P(:,1).').^2 + (P(:,2)-P(:,2).').^2); % distance table from each point to all other points
m = tril(ones(n)); m(m==0) = NaN; % make a triangular mask
Derr = abs(D-d).*m; % calculate error
[Da Na] = min(Derr,[],2); % minimize
[~, Nb] = min(Da,[],1);
% these are the points with distance most closely matching the target
Pa = P(Na(Nb),:)
Pa = 1×2
7.1973 7.1303
Pb = P(Nb,:)
Pb = 1×2
0.8595 9.9292
% show the distance between Pa,Pb
sqrt((Pa(1)-Pb(1)).^2 + (Pa(2)-Pb(2)).^2)
ans = 6.9283

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by