Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Distance from point to set

  • To: mathgroup at
  • Subject: [mg55411] Re: Distance from point to set
  • From: Piotr Kowalski <pkowalsk at>
  • Date: Tue, 22 Mar 2005 03:51:01 -0500 (EST)
  • References: <d1eblq$ek7$>
  • Sender: owner-wri-mathgroup at


Thank you for all your replies.
Here is the summary of different solutions obtained:

Jens-Peer Kuska, kuska at
distanceToSet[pnt_, pset_] := Min[Sqrt[#.#] &[# - pnt] & /@ pset]
Bob Hanlon, hanlonr at
dist1[pt_, ptList_] := Module[{d},
and in version 5
dist2[pt_, ptList_, p_:2] := Norm[pt-#,p]&/@ptList;
Peter Pein, petsie at
In[2]:= (* definition of the distance *)
   d[el_, set_, p_:Infinity] := Min[(Norm[el - #1, p] & ) /@ set]
David Park, djmp at
distance[set_][point_] :=
   Min[(Sqrt[Plus @@ ((point - #1)2)] & ) /@ set]
Andrzej Kozlowski, akoz at
If you have Mathematica 5 or above you can use simply:
dist[p_, l_, n_:2] := Min[Norm[#, n] & /@ Transpose[Transpose[l] - p]]
Steve Luttrell, steve_usenet at
Here is how you would define a function to do this for a Euclidean 
distance measure:

More generally you could pass a 3rd argument to this function to
specify a function for measuring distance:

I have learned here (among others) about nice new function Norm[var,p]
for Mathematica 5.x but unfortunately, I have only access to
Mathematica 4.1 at present.

The solutions presented above are for the discrete set A, containing
finite number of points (list of points in Mathematica).

And what in the case, when  the set A is described by some
linear inequalities (giving for example convex cone)
or nonlinear inequalities and not finite number of points ?
I've got information by private e-mail about NMinimize function
(in Mathematica 5.x) or package MathOptimizer for Mathematica 3.x,4.x,5.x.
Are there any other approaches to such problem in Mathematica?

Thank you in advance,
Piotr Kowalski

> Hello,
> I would like to compute distance d(x,A) from a point 'x' to a set 'A',
> (all in R^n, where n=2 or n=3) that is:
>    d(x,A) = min ||x - a|| (forall a in A)
>    where: n=2 or n=3,
>           x is point in R^n, A is subset of R^n
>           ||  || is norm (euclidean, max, etc).
> Can I find Mathematica function or package for such problem ?
> Thank you in advance,
> P. Kowalski

  • Prev by Date: Re: Restriction on the domain of Plot3D
  • Next by Date: Re: TrigExpand - Odd behavior
  • Previous by thread: Re: Distance from point to set
  • Next by thread: Problem with singularities in Plot3D