Got Questions? Get Answers.
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:
shortest distance between a point and vector (n dimension)

Subject: shortest distance between a point and vector (n dimension)

From: Kevin

Date: 26 Dec, 2011 23:12:08

Message: 1 of 5

I have a direction vector (d) created from two vectors : d=x-g
I have to find the perpendicular distance from a point to line d.
All of above are in n dimensions.
Please help me with this problem how can calculate this distance in Matlab??

Subject: shortest distance between a point and vector (n dimension)

From: John D'Errico

Date: 27 Dec, 2011 01:37:08

Message: 2 of 5

"Kevin " <k.amouzgar@gmail.com> wrote in message <jdav08$ru6$1@newscl01ah.mathworks.com>...
> I have a direction vector (d) created from two vectors : d=x-g
> I have to find the perpendicular distance from a point to line d.
> All of above are in n dimensions.
> Please help me with this problem how can calculate this distance in Matlab??

Basic algebra.

You have a point, call it P, and a line defined by two
other points, x & g.

1. Normalize the vector (x - g) to have unit length.

  d = x - g;
  d = d./norm(d); % normalize to unit length

2. Form the vector (P - g) which translates the problem
so the line essentially passes through the origin.

3. Subtract off the component of that vector that lies
along the line xg. Thus the dot product gives you the
projection of the vector (P - g), onto the vector (x-g).
Multiply by d, and we get the component of (P - g) in
the same direction as our line.

   d*dot(P - g,d)

4. Now, simply subtract this component from (P - g),
and compute the length of the vector that remains.
This remainder is perpendicular to our line.

  norm((P - g) - d*dot(P - g,d))

The norm function computes the distance you desire.

John

Subject: shortest distance between a point and vector (n dimension)

From: Roger Stafford

Date: 27 Dec, 2011 01:54:09

Message: 3 of 5

"Kevin " <k.amouzgar@gmail.com> wrote in message <jdav08$ru6$1@newscl01ah.mathworks.com>...
> I have a direction vector (d) created from two vectors : d=x-g
> I have to find the perpendicular distance from a point to line d.
> All of above are in n dimensions.
> Please help me with this problem how can calculate this distance in Matlab??
- - - - - - - -
  It isn't clear whether you want the distance from a point p to the line between vector endpoints x and g or the distance to the line of vector d with base located at the origin.

  If you mean the former, then points on the line between x and g are traced by the vector t*x+(1-t)*g as parameter t varies from minus infinity to plus infinity. The value of t for which the n-dimensional distance to p is minimum will be:

 t = sum((p-g).*(x-g))/sum((x-g).^2);
 p0 = t*x+(1-t)*g; % The projection of p on the line
 D = sqrt((p-p0).^2);

  If you mean the latter of the above possible meanings, use d and the zero vector in place of x and g in the above.

Roger Stafford

Subject: shortest distance between a point and vector (n dimension)

From: Kevin

Date: 27 Dec, 2011 12:03:08

Message: 4 of 5

thank you for your answer!
To be frank I am writing a code for genetic algorithm optimization, and I needed this for creating offspring from parents.
lets assume g was the mean vector of (mou) number of parents .
and x was one of parents for creating offspring. ( point P was one of the other parents)
For creating the offspring I also need (mou-1: other parents excluding x) orthonormal basis that span the subspace perpendicular to vector d.
Can i use 'orth' function?? How and if not what do you propose?
I again appreciate your help.

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <jdb7g4$lfg$1@newscl01ah.mathworks.com>...
> "Kevin " <k.amouzgar@gmail.com> wrote in message <jdav08$ru6$1@newscl01ah.mathworks.com>...
> > I have a direction vector (d) created from two vectors : d=x-g
> > I have to find the perpendicular distance from a point to line d.
> > All of above are in n dimensions.
> > Please help me with this problem how can calculate this distance in Matlab??
>
> Basic algebra.
>
> You have a point, call it P, and a line defined by two
> other points, x & g.
>
> 1. Normalize the vector (x - g) to have unit length.
>
> d = x - g;
> d = d./norm(d); % normalize to unit length
>
> 2. Form the vector (P - g) which translates the problem
> so the line essentially passes through the origin.
>
> 3. Subtract off the component of that vector that lies
> along the line xg. Thus the dot product gives you the
> projection of the vector (P - g), onto the vector (x-g).
> Multiply by d, and we get the component of (P - g) in
> the same direction as our line.
>
> d*dot(P - g,d)
>
> 4. Now, simply subtract this component from (P - g),
> and compute the length of the vector that remains.
> This remainder is perpendicular to our line.
>
> norm((P - g) - d*dot(P - g,d))
>
> The norm function computes the distance you desire.
>
> John

Subject: shortest distance between a point and vector (n dimension)

From: John D'Errico

Date: 27 Dec, 2011 14:28:08

Message: 5 of 5

"Kevin " <k.amouzgar@gmail.com> wrote in message <jdcc5s$4t0$1@newscl01ah.mathworks.com>...
> thank you for your answer!
> To be frank I am writing a code for genetic algorithm optimization, and I needed this for creating offspring from parents.
> lets assume g was the mean vector of (mou) number of parents .
> and x was one of parents for creating offspring. ( point P was one of the other parents)
> For creating the offspring I also need (mou-1: other parents excluding x) orthonormal basis that span the subspace perpendicular to vector d.
> Can i use 'orth' function?? How and if not what do you propose?

No. Read the help. orth gives you an orthonormal basis that
spans a set of vectors. You are apparently asking for a set
of basis vectors that span the null space, so vectors that
are orthogonal to your vector d.

This is the null space you are looking for, so use the function
null.

John

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