Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <hsqr9u$phf$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1274111224 3446 172.30.248.37 (17 May 2010 15:47:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 17 May 2010 15:47:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:636496

"Ram Lakshmi" <ram.gopal@nsn.com> wrote in message <hsqr9u$phf$1@fred.mathworks.com>...
> 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