Re: On partitioning lists by intervals
- To: mathgroup at smc.vnet.net
- Subject: [mg80502] Re: On partitioning lists by intervals
- From: Donald DuBois <donabc at comcast.net>
- Date: Thu, 23 Aug 2007 06:24:41 -0400 (EDT)
> 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