# Attempting to fill a table with the number of elements in each bin and make a table with the elements in the bins?

I am having some difficulty trying to fill a table with the number of elements in each bin while also making a table that holds the elements in each bin.

Okay, let me go over my thoughts:

So I have a list:

dat = {0,1,3,9,11,13,14,15,19,20};


I want to have each bin be a constant width, which I have set at 5, so I know to find the number of bins:

numBins = (Max[dat] - Min[dat])/width


To get my intervals, I wrote:

intervals = Table[0,2*numBins];
intervals[[1]] = min;
For[i = 2, i < 2*numBins, i+=2, intervals[[i]] = intervals[[i-1]] + width; intervals[[i+1]] = intervals[[i]]]
intervals[[2*numBins]] = intervals[[2*numBins - 1]] + width;


which gets me:

{0, 5, 5, 10, 10, 15, 15, 20}


which is exactly what I'm looking for. Now, I know that for the bins, I should get:

0-5, 5-10, 10-15, 15-20 (intervals)

0    9     11     19    (elements in bins)
1          13     20
3          14
15

3    1     4      2     (number of elements in each bin)


Now, I thought that to get the number of elements in each bin and fill in a bin table, I might do something like (pseudocode-ish)

int k = 0;
numOfElements = Table[0, {i,numBins}]
elements = Table[0, {i, numBins}, {j, Length[dat]}]
for(int i = 2, i < Length[intervals], i+=2){
for(int j = 0, j < Length[dat], j++){
if(dat[[j]] > intervals[[i]] && dat[[j]] < intervals[[i+1]]){
elements[[i]][[j]] = dat[[j]];
count++;
}
numOfElements[[k]] = count;
k++;
count = 0;
}}


What I'm having problems with is 1) is this the correct logic? and 2) implementing in Mathematica. Any help in tackling this problem would be greatly appreciated.

EDIT: @kglr pointed out a lot of good stuff and I also noticed BinCounts, which is useful, but I noticed that

BinLists[dat, 5] /. {}->Nothing


gets me

{{0, 1, 3}, {9}, {11, 13, 14}, {15, 19}, {20}}


when what I'm looking to try to get is

{{0, 1, 3}, {9}, {11, 13, 14, 15}, {19, 20}}


So while BinLists gets me something close, it's not quite what I'm trying to do.

• – kglr
Jul 5 '18 at 22:50
• @kglr Jeeze, yep, BinLists. Curious though - do you have any advice on how to set and appropriate x_max, because Length[dat] isn't nearly long enough. Jul 5 '18 at 22:55
• Jomy, maybe you can do what BinLists does: Binlists >> Details : _BinLists[data,dx] takes the bin boundaries to be integer multiples of dx, with the first bin starting at Ceiling[Min[data]-dx,dx] and the last bin ending at Floor[Max[data]+dx,dx]. _
– kglr
Jul 5 '18 at 22:59

You can use BinLists and HistogramList as follows:

dat = {0, 1, 3, 9, 11, 13, 14, 15, 19, 20};

BinLists[dat, 5]


{{}, {0, 1, 3}, {9}, {11, 13, 14}, {15, 19}, {20}}

{binlims, bincounts} = HistogramList[dat, {5}]


{{-5, 0, 5, 10, 15, 20, 25}, {0, 3, 1, 3, 2, 1}}

Update: To get {{0, 1, 3}, {9}, {11, 13, 14, 15}, {19, 20}} as the result you can specify explicit bin limits (e.g.,{{0, 5, 10, 16, 20}} or {{0, 5, 10, 15 + .0001 , 20}}) as the second argument of BinLists:

BinLists[dat, {{0, 5, 10, 16, 25}}]


{{0, 1, 3}, {9}, {11, 13, 14, 15}, {19, 20}}

• Interesting! Can you explain why BinLists gives this empty {} and why HistogramList gives this -5 and 0 term? Is there a way to get rid of those? Jul 5 '18 at 22:56
• @JomyBlue, you get {} when a bin is empty. In this case the first bin (the interval from -5 to 0) is empty. To get rid of them you replace them with Nothing: that is, BinLists[dat, 5] /. {}->Nothing
– kglr
Jul 5 '18 at 23:03
• @JomyBlue Alternatively, you can specify that the first bin should start at the minimum value of the list: dx = 5; BinLists[dat, {Min[dat], Max[dat] + dx, dx}] returns {{0, 1, 3}, {9}, {11, 13, 14}, {15, 19}, {20}}. Jul 5 '18 at 23:04
• Thanks for telling me that! Jul 5 '18 at 23:04
• Huh, I just noticed that this puts 15 in bin 3, when, for my purposes, I want 15 in bin 2. Jul 5 '18 at 23:05