フィルターのクリア

Find the nearest point from a matrix to a particular point.

1 回表示 (過去 30 日間)
Rachel
Rachel 2014 年 2 月 19 日
編集済み: Azzi Abdelmalek 2014 年 2 月 19 日
Hi
I have a matrix which consists of 10 pixel co-ordinates. I need to find the co-ordinates of the closest pixel from the matrix to a particular point say A(x,y).
Can anyone please help? Thanks in advance!

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 19 日
編集済み: Azzi Abdelmalek 2014 年 2 月 19 日
P=randi(100,10,2) % Example
A=[25 5];
c=bsxfun(@minus,P,A)
[out,idx]=min(hypot(c(:,1),c(:,2)))
% The point is
P(idx,:)
You can check the result
scatter(P(:,1),P(:,2))
hold on
scatter(A(1),A(2),'r')
plot([A(1) P(idx,1)],[A(2), P(idx,2)])
hold off
Another way to do it
P=randi(60,10,2) % Example
A=[25 35];
[pmin,idx]=min(sum(bsxfun(@minus,P,A).^2,2))
% The point is
out=P(idx,:)

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by