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.
$\begingroup$
$\endgroup$
2
-
$\begingroup$ Why not use the second argument of HistogramList and specify exactly which bins you want? $\endgroup$– bill sCommented 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$– user27119Commented Oct 23, 2018 at 21:13
Add a comment
|
2 Answers
$\begingroup$
$\endgroup$
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}}
$\begingroup$
$\endgroup$
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.