Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to compute minimum distance between a point and a vector

Subject: How to compute minimum distance between a point and a vector

From: Ram Lakshmi

Date: 17 May, 2010 07:30:06

Message: 1 of 4

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.

Subject: How to compute minimum distance between a point and a vector

From: Airballman

Date: 17 May, 2010 08:10:08

Message: 2 of 4

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

Hi,

Couldn't the nearest_point file help you? It seems like it is roughly what you are trying to do :)

Here is the link :
http://www.mathworks.com/matlabcentral/fileexchange/8939-nearestpoint



C U

Subject: How to compute minimum distance between a point and a vector

From: Ram Lakshmi

Date: 17 May, 2010 15:22:05

Message: 3 of 4

"Airballman " <airballman@hotmail.com> wrote in message <hsqtl0$ehc$1@fred.mathworks.com>...
> "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.
>
> Hi,
>
> Couldn't the nearest_point file help you? It seems like it is roughly what you are trying to do :)
>
> Here is the link :
> http://www.mathworks.com/matlabcentral/fileexchange/8939-nearestpoint
>
>
>
> C U

Hello,

The following the code that I wrote, and the results are coming differently with nearestpoint.

N = 1000;
vec_A = rand(N,2); % random N points
pt = rand(1, 2);% Point
min_distance = 100; % start with some random value as intial distance

for index = 1: N
    dis = sqrt(( (vec_A(index,1) - pt (1,1)) ^2) + ((vec_A(index,2) - pt (1,2))^2));
    dis
    if (dis < min_distance)
        min_distance = dis;
    end
end
sprintf ('Minimum distance from the point p to vec is [%d]', min_distance)

Any help would be appreciated.

Thanks and Regards
R

Subject: How to compute minimum distance between a point and a vector

From: Roger Stafford

Date: 17 May, 2010 15:47:04

Message: 4 of 4

"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us