Services & ResourcesWolfram Forums
 MathGroup Archive
 2006 January February March April May June July August September October November December

RE: programming problem about elements taken

• To: mathgroup at smc.vnet.net
• Subject: [mg72480] RE: [mg72470] programming problem about elements taken
• From: "David Park" <djmp at earthlink.net>
• Date: Sun, 31 Dec 2006 05:31:06 -0500 (EST)

Barrow,

It's not completely clear what your grouping criterion is. After all, the
skull bone is connected to the toe bone but they are quite far apart.

I'm going to use a criterion of sorting all the numbers and then splitting
them into groups whenever there is a gap greater than or equal to epsilon.

Here is a test set.

testset = Table[Random[], {20}]
{0.854031, 0.1915, 0.671011, 0.875499, 0.0538857, 0.420417, 0.0711807,
0.240185, 0.672597, 0.0146134, 0.640515, 0.790636, 0.742558, 0.540169,
0.136822, 0.0476439, 0.725082, 0.482359, 0.414704, 0.411541}

We sort the testset.

step1 = Sort[testset]
{0.0146134, 0.0476439, 0.0538857, 0.0711807, 0.136822, 0.1915, 0.240185,
0.411541, 0.414704, 0.420417, 0.482359, 0.540169, 0.640515, 0.671011,
0.672597, 0.725082, 0.742558, 0.790636, 0.854031, 0.875499}

Pick an epsilon of 1/4 the maximum gap.

epsilon = Max[step2]/4
0.075833

Now use the Split command to group numbers whenever the gap is less than
epsilon.

Split[step1, #2 - #1 < epsilon &]
Length[%]
{{0.0146134, 0.0476439, 0.0538857, 0.0711807, 0.136822, 0.1915,
0.240185}, {0.411541, 0.414704, 0.420417, 0.482359, 0.540169},
{0.640515,
0.671011, 0.672597, 0.725082, 0.742558, 0.790636, 0.854031, 0.875499}}
3

If that is what you want, then write and document a routine for your custom
use.

groupValues::usage =
"groupValues[epsilon][valuelist] will sort and group the numbers in \
valuelist whenever the gap between two successive values is greater than or
\
equal to epsilon.";
groupValues[epsilon_?Positive][valuelist : {__?NumericQ}] :=
Split[Sort[valuelist], #2 - #1 < epsilon &]

testset // groupValues[epsilon]
Length[%]
{{0.0146134, 0.0476439, 0.0538857, 0.0711807, 0.136822, 0.1915,
0.240185}, {0.411541, 0.414704, 0.420417, 0.482359, 0.540169},
{0.640515,
0.671011, 0.672597, 0.725082, 0.742558, 0.790636, 0.854031, 0.875499}}
3

It will almost always be worthwhile to write routines to extend Mathematica
to conveniently do the things you want. Sometimes they might be quite
extensive, and sometimes, as in this example, they might be simple
combinations that are easier to use and save typing. If you build up sets of
such routines you will soon find that Mathematica is much more powerful and
convenient.

David Park

From: Barrow [mailto:GRseminar at gmail.com]

Dear all,
I have a list of numbers (A),in fact, they are numbers distributed
over [0,1].  Given parameter \epsilon, I have to choose elements from A
such that their distances are larger than \epsilon, so the elements are
"distringuishable". My goal is to find the maximum number of elements
from A to meet the above "distinct criterion".
How to do it with the functions build-in in mathematica ??