필터 지우기
필터 지우기

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 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