Re: Interpoint distances
- To: mathgroup at smc.vnet.net
- Subject: [mg41339] Re: [mg41327] Interpoint distances
- From: Bobby Treat <drmajorbob at mailblocks.com>
- Date: Thu, 15 May 2003 04:02:49 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
<< DiscreteMath`Combinatorica` distance = Sqrt[#.#] &@(#1 - #2) &; t = Table[Random[], {5}, {2}]; distance @@@ KSubsets[t, 2] Bobby -----Original Message----- From: DIAMOND Mark R. <dot at dot.dot> To: mathgroup at smc.vnet.net Subject: [mg41339] [mg41327] Interpoint distances 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