Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

On partitioning lists by intervals

1 view
Skip to first unread message

Mauricio Esteban Cuak

unread,
Aug 21, 2007, 5:10:15 AM8/21/07
to
Hello there.
I'm just starting in mathematica and I realise this is a very basic
question, but I tried to find an answer on the documentation center
for a couple of hours and on the archive for mathgroup, but couldn't
find it.
I have a list of say 500 different random values. I need to divide it
in n intervals of fixed length (for example, the lowest value is 0 and
the maximum 100 so I need to get sublists of values that go from 0 to
10, 10 to 20, etc.)
Thanks for your help!

cd

David Annetts

unread,
Aug 22, 2007, 4:38:26 AM8/22/07
to
Hi Mauricio

... You want to bin your data into particular intervals.

The easiest way is using BinLists. First, generate some data

data = RandomReal[{0, 100}, 500];
Length@data

... And bin them

datab = BinLists[data, 10];

If you only want to count the number of samples in a particular bin (ie for
a histogram), you can use BinCounts or you can Map[] the function Length[]
to each element of the array datab

binc = Length[#]& /@ datab
SameQ[binc, BinCounts[data, 10]]

Regards,

Dave.

DrMajorBob

unread,
Aug 22, 2007, 4:51:02 AM8/22/07
to
data = RandomInteger[{1, 100}, 200];

BinCounts[data, {0, 100, 10}]

{20, 11, 20, 25, 21, 21, 17, 24, 21, 19}

BinLists[data, {0, 100, 10}]
Length /@ %

{{1, 9, 6, 9, 2, 2, 2, 4, 8, 8, 7, 4, 7, 1, 5, 6, 5, 2, 7, 3}, {19,
14, 19, 12, 18, 15, 11, 15, 12, 11, 11}, {25, 20, 24, 22, 26, 29,
29, 27, 20, 26, 20, 27, 25, 23, 24, 24, 25, 23, 26, 24}, {36, 36,
36, 32, 35, 38, 39, 33, 31, 31, 37, 33, 31, 38, 31, 30, 34, 36, 33,
37, 37, 32, 37, 37, 32}, {45, 41, 46, 40, 44, 45, 40, 48, 43, 47,
46, 49, 42, 41, 46, 45, 41, 43, 44, 43, 40}, {57, 59, 52, 57, 55,
58, 55, 50, 56, 55, 53, 52, 50, 59, 55, 55, 52, 57, 53, 51,
54}, {67, 61, 64, 66, 62, 63, 66, 64, 65, 62, 61, 63, 63, 61, 65,
66, 63}, {71, 78, 77, 73, 75, 70, 75, 76, 70, 72, 76, 78, 79, 74,
70, 72, 71, 78, 78, 71, 70, 72, 76, 77}, {82, 85, 87, 86, 88, 82,
87, 84, 82, 85, 81, 88, 85, 80, 86, 82, 80, 83, 89, 89, 85}, {95,
92, 90, 90, 96, 96, 96, 93, 98, 94, 92, 91, 95, 94, 91, 93, 92, 93,
90}}

{20, 11, 20, 25, 21, 21, 17, 24, 21, 19}

Bobby

On Tue, 21 Aug 2007 04:03:41 -0500, Mauricio Esteban Cuak
<cuak...@gmail.com> wrote:

> Hello there.


> I'm just starting in mathematica and I realise this is a very basic
> question, but I tried to find an answer on the documentation center
> for a couple of hours and on the archive for mathgroup, but couldn't
> find it.
> I have a list of say 500 different random values. I need to divide it
> in n intervals of fixed length (for example, the lowest value is 0 and
> the maximum 100 so I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc.)
> Thanks for your help!
>

> cd
>
>

--
DrMaj...@bigfoot.com

Andrzej Kozlowski

unread,
Aug 22, 2007, 5:08:57 AM8/22/07
to

On 21 Aug 2007, at 11:03, Mauricio Esteban Cuak wrote:

> Hello there.
> I'm just starting in mathematica and I realise this is a very basic
> question, but I tried to find an answer on the documentation center
> for a couple of hours and on the archive for mathgroup, but couldn't
> find it.
> I have a list of say 500 different random values. I need to divide it
> in n intervals of fixed length (for example, the lowest value is 0 and
> the maximum 100 so I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc.)
> Thanks for your help!
>
> cd
>


Using Mathematica 6.0:

ls = RandomInteger[{0, 100}, {30}]
{51, 75, 6, 78, 43, 80, 9, 12, 65, 10, 26, 71, 49, 53, 39, 67, 98,
62, 12, \
43, 70, 38, 51, 13, 45, 75, 52, 0, 70, 77}

BinLists[ls, 10]
{{}, {6, 9, 0}, {12, 10, 12, 13}, {26}, {39, 38}, {43, 49, 43, 45},
{51, 53,
51, 52}, {65, 67, 62}, {75, 78, 71, 70, 75, 70, 77}, {80}, {98}}


Andrzej Kozlowski

Jens-Peer Kuska

unread,
Aug 22, 2007, 5:12:03 AM8/22/07
to
Hi,

if you need a fixed length of the sub lists
lst=Table[Random[],{100}];

Partition[Sort[lst],10]

will help. But this is different from


"I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc"

BinCounts[lst,{0,1,0.1}]

Regards
Jens

Jean-Marc Gulliet

unread,
Aug 22, 2007, 5:13:04 AM8/22/07
to
Mauricio Esteban Cuak wrote:
<snip>

> I have a list of say 500 different random values. I need to divide it
> in n intervals of fixed length (for example, the lowest value is 0 and
> the maximum 100 so I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc.)

One way to do that is with Select and Table, testing interval membership
and sorting (if needed) the values. For instance,

In[1]:= data = RandomReal[{0, 100}, {50}];
data = Sort@data;
Table[Select[data, IntervalMemberQ[Interval[{m, m + 10}], #] &], {m,
0, 90, 10}]

Out[3]= {{2.64545, 2.88612, 6.95366, 8.94619}, {}, {21.2125, 22.5317,
23.3286, 23.5521, 27.0041, 27.0368}, {31.8472, 34.0783, 35.9797,
38.0444}, {40.1175, 40.9223, 44.6731, 46.3196, 47.9082, 48.3849,
49.7482}, {50.6901, 51.3209, 52.0433, 53.6529, 57.0734,
59.8382}, {60.2186, 62.8556, 64.1785, 66.705, 68.0119,
68.1914}, {70.2759, 73.2949, 75.0208, 75.026, 75.269, 76.8917,
77.0392}, {80.4942, 85.8137, 87.242, 87.5028, 88.4888,
89.8753}, {91.4804, 93.1612, 98.4789, 99.0704}}

--
Jean-Marc

Bob Hanlon

unread,
Aug 23, 2007, 12:02:24 AM8/23/07
to
data = RandomInteger[{0, 100}, 500];

p1 = Table[Select[data, 10*n <= # < 10*(n + 1) &], {n, 0, 10}];

p2 = Table[Cases[data, _?(10*n <= # < 10*(n + 1) &)], {n, 0, 10}];

p3 = BinLists[data, {0, 110, 10}];

p1 == p2 == p3

True


Bob Hanlon

---- Mauricio Esteban Cuak <cuak...@gmail.com> wrote:
> Hello there.
> I'm just starting in mathematica and I realise this is a very basic
> question, but I tried to find an answer on the documentation center
> for a couple of hours and on the archive for mathgroup, but couldn't
> find it.

> I have a list of say 500 different random values. I need to divide it
> in n intervals of fixed length (for example, the lowest value is 0 and
> the maximum 100 so I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc.)

Andrzej Kozlowski

unread,
Aug 23, 2007, 12:03:26 AM8/23/07
to

On 21 Aug 2007, at 11:03, Mauricio Esteban Cuak wrote:

> Hello there.
> I'm just starting in mathematica and I realise this is a very basic
> question, but I tried to find an answer on the documentation center
> for a couple of hours and on the archive for mathgroup, but couldn't
> find it.
> I have a list of say 500 different random values. I need to divide it
> in n intervals of fixed length (for example, the lowest value is 0 and
> the maximum 100 so I need to get sublists of values that go from 0 to
> 10, 10 to 20, etc.)
> Thanks for your help!
>
> cd
>

Donald DuBois

unread,
Aug 23, 2007, 1:41:05 AM8/23/07
to
> Hello there.
> I'm just starting in mathematica and I realise this
> is a very basic
> question, but I tried to find an answer on the
> documentation center
> for a couple of hours and on the archive for
> mathgroup, but couldn't
> find it.
> I have a list of say 500 different random values. I
> need to divide it
> in n intervals of fixed length (for example, the
> lowest value is 0 and
> the maximum 100 so I need to get sublists of values
> that go from 0 to
> 10, 10 to 20, etc.)
> Thanks for your help!
>
> cd
>

Since the lowewt value is 0 and the highest is 100
there are 101 possible values which is not divided evenly
by an interval of 10. Suppose the values go from 1 to 100 with a fixed interval of 10 then the following should
work.

lst = RandomInteger[{1,100}, 500];
BinCounts[lst, {Range[1,101,10]}]

{43, 45, 44, 53, 50, 59, 53, 52, 57, 44}

The intervals are 1 to 10, 11 to 20, ... 91 to 100.

Hope that helps,
Don

Donald DuBois

unread,
Aug 23, 2007, 6:27:43 AM8/23/07
to
> Hello there.
> I'm just starting in mathematica and I realise this
> is a very basic
> question, but I tried to find an answer on the
> documentation center
> for a couple of hours and on the archive for
> mathgroup, but couldn't
> find it.
> I have a list of say 500 different random values. I
> need to divide it
> in n intervals of fixed length (for example, the
> lowest value is 0 and
> the maximum 100 so I need to get sublists of values
> that go from 0 to
> 10, 10 to 20, etc.)
> Thanks for your help!
>
> cd
>

I think I did not interpret your message correctly
the first time.

data = Sort[RandomInteger[{0, 100}, 500]];

To count the number of values that fall into each interval:

BinCounts[data, {0, 110, 10}]
{43, 49, 53, 47, 39, 52, 51, 53, 47, 61, 5}

Notice that the max bin is 110 (max value (100) + interval (10))
and not 100. That is because BinCounts uses
as an interval for each value:

minInterval <= value < maxInterval.

Therefore, to count the number of 100's in the list requires an extra bin.

To form sublists of the data partitioned by interval size:

BinLists[data, {0, 110, 10}]

where the maximum bin size is also 110 for the same reason.

The Sort around RandomInteger is not strictly required,
(BinCounts and BinLists will still work) but the sublists produced by BinLists are easier
to read if the numbers are in ascending (or descending) order.

Regards,
Don

0 new messages