Re: Efficient Histogram Algorithm?
- To: mathgroup at smc.vnet.net
- Subject: [mg113142] Re: Efficient Histogram Algorithm?
- From: Julian Francis <julian.w.francis at gmail.com>
- Date: Fri, 15 Oct 2010 13:50:41 -0400 (EDT)
- References: <i9276l$3fg$1@smc.vnet.net>
Have gone with jhistogram3:
jhistogram3[list_] :=
Sort[Tally[Join[Range[0, 255], list]]][[All, 2]] - 1
In[96]:= Timing[hist3 = jhistogram3[randomList];][[1]]
Out[96]= 0.015
In[98]:= tallybased =
With[{tallies = Tally[randomList]},
Split[Sort[
Join[tallies,
Transpose[{Range[0, 255],
ConstantArray[0,
256]}]]], #[[1]] == #2[[1]] &][[All, -1, -1]]]; // Timing
Out[98]= {0.016, Null}
It is based on Bob's solution, but incorporates (insight from Darren)
a fix where if there were missing values in the random array, it
wouldn't have returned a value (of 0) for that.
The Range ensures that all values in the histogram will return, then I
subtract one from each of the histogram buckets to compensate for the
fact that I put an extra one in in the
first place.
They perform pretty similarly, I just find the first one easier for me
to understand.
Thanks all.
Julian.