Re: Counting number of numbers in a large list between two valus
- To: mathgroup at smc.vnet.net
- Subject: [mg114544] Re: Counting number of numbers in a large list between two valus
- From: Lyle Gordon <lgordon at gmail.com>
- Date: Tue, 7 Dec 2010 06:46:51 -0500 (EST)
Again, thanks to everyone who responded. After some playing around it seems that since I need to run the code a number of times on the same set of data and my ranges only have a certain degree of precision this code might be faster: binwidth = 10^-5; min = Floor[Min[data], binwidth]; max = Ceiling[Max[data], binwidth]; binneddata = BinCounts[data, {min, max, binwidth}]; tallydata4[{x1_, x2_}] := Total[Take[ binneddata, {(x1 - min)/binwidth + 1, (x2 - min)/binwidth}]] Thanks, Lyle -- Lyle Gordon Department of Materials Science and Engineering Northwestern University 2220 Campus Drive Evanston, IL 60208 Tel: (847) 491-3584 Mobile: (847) 400-4071 lgordon at u.northwestern.edu On Mon, Dec 6, 2010 at 11:27 AM, Lyle Gordon <lgordon at gmail.com> wrote: > Thanks everyone for your responses, it seems like Carl's and Fred's method > with Total-Unitize-Clip is the fastest. > > Thanks very much, > Lyle > > -- > Lyle Gordon > Department of Materials Science and Engineering > Northwestern University > > 2220 Campus Drive > Evanston, IL 60208 > > Tel: (847) 491-3584 > Mobile: (847) 400-4071 > lgordon at u.northwestern.edu > > > > On Mon, Dec 6, 2010 at 5:33 AM, Leonid Shifrin <lshifr at gmail.com> wrote: > >> Hi Lyle, >> >> Sorry - I misunderstood the problem. I thought you meant the positional >> arrangement >> of the numbers. Since you seem to be getting plenty of good answers, I >> will not attempt >> to correct myself with more code. >> >> >> Regards, >> Leonid >> >> >> On Mon, Dec 6, 2010 at 5:57 AM, Lyle <lgordon at gmail.com> 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 >>> >>> >> >