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

2 views (last 30 days)
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!

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Feb 2014
Edited: Azzi Abdelmalek on 19 Feb 2014
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,:)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!