Re: breaking up lists into intervals

• To: mathgroup at smc.vnet.net
• Subject: [mg23088] Re: breaking up lists into intervals
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Sun, 16 Apr 2000 00:37:30 -0400 (EDT)
• References: <8d964f\$ie1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Matt,
I have not tested the following for speed, and it may be more general than
you need.

bin[data_, tests_] :=
Last /@ Transpose /@
Split[Sort[{Position[Append[tests, True &], t_ /; t[#], {1}, 1][[1,
1]], #} & /@ data], #[[1]] == #2[[1]] &]

data = Range[15];
tests = {14 > # > 9 &, # < 12 &};

bin[data, tests]

{{10, 11, 12, 13}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {14, 15}}

The last sublist is of those that fail all tests.

data2 = Table[Random[Integer, {1, 12}], {12}, {3}]

{{4, 8, 10}, {10, 9, 5}, {5, 8, 10}, {5, 7, 8}, {8, 8, 11}, {12, 9, 8}, {1,
11, 10}, {1, 11, 2}, {6, 10, 8}, {1, 9, 7}, {3, 12, 3}, {1, 9, 4}}

bin[data2,
{#[[1]] <= 4 &, #[[1]] <= 8 &}]

{{{1, 9, 4}, {1, 9, 7}, {1, 11, 2}, {1, 11, 10}, {3, 12, 3}, {4, 8, 10}},
{{5,
7, 8}, {5, 8, 10}, {6, 10, 8}, {8, 8, 11}}, {{10, 9, 5}, {12, 9, 8}}}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

<Matt.Johnson at autolivasp.com> wrote in message
news:8d964f\$ie1 at smc.vnet.net...
>
>
> Dear Mathgroup:
>
> I have many large datasets of {x,y,z} data that I wish to break into small
data
> sets based on the value of x.  For example, the x value ranges from 0 to
100 and
> I want ot break up the data into 20 groups, from 0-5, 5-10, 10-15, etc.
There
> will be an unequal number of data points in each interval.  I have written
a
> routine based on several Do loops to do this and it works satisfactorily.
> However, I would think that there is a way to eliminate from the data set
the
> points that have already been placed in their appropriate intervals, or a
> routine that would "place" the point in the appropriate group, only having
to go
> through the datasets once.  Either of these options would speed up the
process.
> Currently the routine goes through each complete dataset as many times as
there
> are intervals created.  Here is the current code:
>
> Do[Do[Do[
>      If[ i-0.5 di<=dataset[j][[k,1]]<=i+0.5 di,
>      AppendTo[group[j,i],dataset[j][[k]] ]],
>      {k, Length[dataset[j]]}],
>      {i, imin, imax, di}], {j,jmax}]
>
> There are j datasets with k points in each dataset.  i serves as the index
for
> the intervals, according to the x value, with an interval size of di.
> It creates (imax-imin)/di intervals in each dataset.
>
> Thanks for any help
>
> Matt Johnson
>
>
>

```

• Prev by Date: Re: breaking up lists into intervals
• Next by Date: Re: Keyboard shortcuts
• Previous by thread: Re: breaking up lists into intervals
• Next by thread: Re: breaking up lists into intervals