2
$\begingroup$

HistogramList provides a way to specify number of bins. How do I specify the number of bins in BinLists?

$\endgroup$
1
  • $\begingroup$ For n bins (n > 1): BinLists[data, (Max[data] - Min[data])/(n - 1)] $\endgroup$
    – Bob Hanlon
    Oct 1, 2016 at 22:00

1 Answer 1

2
$\begingroup$
ClearAll[binListsF];
binListsF[data_, binning_: Automatic] := 
 HistogramList[data, binning, BinLists[data, {#1}] &]

Examples:

SeedRandom[1];
data = RandomVariate[NormalDistribution[0, 1], 20];

binListsF[data]

{{-2, -1, 0, 1, 2},
{{-1.29575, -1.76175}, {-0.416754, -0.0148679, -0.191385, -0.34766, -0.185635}, {0.485679, 0.40914, 0.261881, 0.63566, 0.676805, 0.1307, 0.0206518, 0.0977624, 0.4083}, {1.5443, 1.09863, 1.31189, 1.51306}}}

binListsF[data, 3]

{{-2, 0, 2},
{{-0.416754, -1.29575, -0.0148679, -1.76175, -0.191385, -0.34766, -0.185635}, {0.485679, 0.40914, 0.261881, 0.63566, 1.5443, 0.676805, 0.1307, 1.09863, 0.0206518, 1.31189, 0.0977624, 0.4083, 1.51306}}}

binListsF[data, 10]

{{-2, -(3/2), -1, -(1/2), 0, 1/2, 1, 3/2, 2},
{{-1.76175}, {-1.29575}, {}, {-0.416754, -0.0148679, -0.191385, -0.34766, -0.185635}, {0.485679, 0.40914, 0.261881, 0.1307, 0.0206518, 0.0977624, 0.4083}, {0.63566, 0.676805}, {1.09863, 1.31189}, {1.5443, 1.51306}}}

Modify the definition to get the the list of binned elements to

 HistogramList[data, binning, BinLists[data, {#1}] &][[2]]

Alternatively, you can use

ClearAll[binListsF2];
binListsF2[data_, binning_: Automatic] := 
 BinLists[data, {HistogramList[data, binning][[1]]}]

{binListsF2[data] == binListsF[data][[2]], binListsF2[data, 3] == binListsF[data, 3][[2]]}

{True, True}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.