From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to compute minimum distance between a point and a vector
Date: Mon, 17 May 2010 15:47:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <hsrodo$3bm$>
References: <hsqr9u$phf$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1274111224 3446 (17 May 2010 15:47:04 GMT)
NNTP-Posting-Date: Mon, 17 May 2010 15:47:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:636496

"Ram Lakshmi" <> wrote in message <hsqr9u$phf$>...
> Hello,
> I have a point pt( x,y) and an array A(X,Y). 
> The array A is around 1000 or more elements.
> I wanted to know the element in A that gives the shortest distance to the point pt.
> I tried to use the for loop and computed  the normal distance formula but it takes longer compute time. 
> Is there any inbuilt function to do this? Can someone share the code.
> Thanks and Regards
> R.

  You can find the minimum distance this way:

 [d,p] = min((A(:,1)-x).^2 + (A(:,2)-y).^2);
 d = sqrt(d);

The d variable will be your shortest distance and the p index indicates the row in A where the nearest point to (x,y) is located.  (I am assuming A is an array with two columns in which each row contains the x and y coordinates of a point to be compared with the point (x,y).  In that sense A is a "vector" of points, though matlab considers it an array.)

Roger Stafford