Find the nearest point from a matrix to a particular point.
3 次查看(过去 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!
0 个评论
回答(1 个)
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,:)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!