Re: Distance from point to set

*To*: mathgroup at smc.vnet.net*Subject*: [mg55314] Re: Distance from point to set*From*: Peter Pein <petsie at arcor.de>*Date*: Sat, 19 Mar 2005 04:45:15 -0500 (EST)*References*: <d1eblq$ek7$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Piotr Kowalski wrote: > 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 > Hello Piotr, Mathematica has got a builtin Norm[]: In[1]:= (* genrating testing data *) x = {1, 2, 3}; A = Table[Random[Integer, {-5, 10}], {5}, {3}] Out[1]= {{-5, -3, 8}, {4, -1, 2}, {-5, -4, 6}, {-5, 10, 10}, {-3, 10, 5}} In[2]:= (* definition of the distance *) d[el_, set_, p_:Infinity] := Min[(Norm[el - #1, p] & ) /@ set] (* you can use 2 in place of Infinity to get euclidian norm as default *) In[3]:= d[x, A] Out[3]= 3 In[4]:= (* the same function works for R^2*) d[{1, 1}, {{-1, 1}, {2, 2}, {2, 1}}, 2] Out[4]= 1 -- Peter Pein Berlin