0
$\begingroup$

Is there a native Mathematica function, similar to HistogramList[...] which will return a list of bin centres and associated counts, rather than the bin domains. I guess it is probably a quick thing to write a custum function based on HistogramList[...] but if there is already a native function for this, then that would be great.

$\endgroup$
2
  • $\begingroup$ Why not use the second argument of HistogramList and specify exactly which bins you want? $\endgroup$
    – bill s
    Oct 23, 2018 at 21:09
  • $\begingroup$ @bills Because I don't always know that depending on my data type, I specify the number of bins either by some $n^{1/3}$ rule or some of the other histogram bin number algorithm. $\endgroup$
    – user27119
    Oct 23, 2018 at 21:13

2 Answers 2

2
$\begingroup$

If I understand the question, instead of:

list = HistogramList[RandomReal[{0, 10}, 100]]
{{0, 2, 4, 6, 8, 10}, {20, 17, 14, 27, 22}}

you want:

{MovingAverage[list[[1]], 2], list[[2]]}
{{1, 3, 5, 7, 9}, {23, 24, 25, 17, 11}}

or maybe you would prefer:

Thread[{MovingAverage[list[[1]], 2], list[[2]]}]
{{1, 23}, {3, 24}, {5, 25}, {7, 17}, {9, 11}}
$\endgroup$
1
$\begingroup$

A more fragile approach but quicker if one can assume a common binwidth:

SeedRandom[12345];
list = HistogramList[RandomReal[{0, 10}, 100]]
(* {{0,2,4,6,8,10},{20,16,27,28,9}} *)

binwidth = list[[1, 2]] - list[[1, 1]];
{list[[1, Range[Length[list[[1]]] - 1]]] + binwidth/2, list[[2]]}
(* {{1,3,5,7,9},{20,16,27,28,9}} *)

But any additional speed is probably minuscule compared to getting the histogram counts in the first place.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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