Re: Interpoint distances
- To: mathgroup at smc.vnet.net
- Subject: [mg41344] Re: Interpoint distances
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Thu, 15 May 2003 04:05:03 -0400 (EDT)
- Organization: University of Washington
- References: <b9tdnj$7r0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Mark, Here is one idea: distlist[t_]:=Block[{c={}}, Nest[ (c={c,Sqrt[Plus@@((Transpose[Rest[#]]-First[#])^2)]};Rest[#])&, t, Length[t]-1 ]; Flatten[c] ] This function is quite a bit faster than yours with large data sets (almost 2 orders of magnitude for your test case on my machine, a 1.9GHz Pentium), and scales as O(n^2). Carl Woll Physics Dept U of Washington "DIAMOND Mark R." <dot at dot.dot> wrote in message news:b9tdnj$7r0$1 at smc.vnet.net... > I am trying to find an efficient method of calculating all the pairwise > (Euclidean) interpoint distances in a given list of points in R^2. I am sure > that if my matrix algebra were any good, this would be solvable in a better > manner than I have done it. Ideally, I would like to count each pair of > points only once, and not count points paired with themselves.I've searched > the archive, and tried the obvious combinations of words on Google, but no > luck. > > My slow method (but the fastest of those I've tried) is > > (* Define a distance function for a pair of points *) > distance[{{x0_, y0_}, {x1_, y1_}}] := Module[ > { > xd = x0 - x1, > yd = y0 - y1 > }, > Sqrt[xd^2 + yd^2] > ] > > (* Create a list of random points with which to experiment *) > t=Table[{Random[], Random[]}, {1024}] > > (* Union in the next line is just used to get rid of all the duplicates, and > to dump all but one of the 0 interpoint distances between a point and itself > *) > interpointDistances = Union[Map[distance, Flatten[Outer[List, t, t, 1], > 1]]]; > > I would be very grateful for any suggestions for improvement. > > Cheers, > > Mark > -- > Mark R. Diamond > > > > > >