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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Computational Geometry 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by