Re: Counting number of numbers in a large list between two valus
- To: mathgroup at smc.vnet.net
- Subject: [mg114539] Re: Counting number of numbers in a large list between two valus
- From: "Kevin J. McCann" <Kevin.McCann at umbc.edu>
- Date: Tue, 7 Dec 2010 06:45:54 -0500 (EST)
- References: <idhjda$9f6$1@smc.vnet.net>
The fastest I got is
data = RandomReal[UniformDistribution[{0, 1}], 10000000];
xmin = 0.5;
xmax = 0.6;
Timing[Total[UnitStep[(data - xmin)*(xmax - data)]]]
{0.25, 1001405}
On 12/5/2010 9:56 PM, Lyle wrote:
> Dear Listers,
>
> I have a large (5-20million) one dimensional list of real numbers and
> I want to count the number of entries in the list that lie between 2
> specific values (x1, x2). I need to run the function for a number of
> different ranges.
>
> ie. number of list entries (l), where x1<= l<= x2
>
> I've tried:
>
> tallydata[{x1_, x2_}] := Count[data, x_ /; x1<= x<= x2]
>
> that takes about 3-4 seconds
>
> and
>
> tallydata[{x1_, x2_}] := Length[Select[data, x1<= #<= x2&]]
>
> which takes a little bit longer.
>
> The best I've managed is (this last one might be off by 1 or 2 but
> this doesn't really matter to me):
>
> sorteddata = Sort[data];
> nf = Nearest[sorteddata];
> tallyrange[{x1_, x2_}] :=
> First[Position[sorteddata, First[nf[x2]]]] -
> First[Position[sorteddata, First[nf[x1]]]]
>
> which takes between 1 and 2 seconds but I was hoping there might be a
> faster way to do this?
>
> Any help would be great!
>
> Thanks,
> Lyle Gordon
>
> Northwestern University
>