Re: Re: Counting list elements above/below a given value
- To: mathgroup at smc.vnet.net
- Subject: [mg17432] Re: [mg17271] Re: Counting list elements above/below a given value
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 9 May 1999 04:43:46 -0400
- References: <7g0qv1$drj@smc.vnet.net> <199904300634.CAA21396@smc.vnet.net.> <7gfu2e$6je@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Carl, Hello Carl, Your finding that x>.5 is a slower test than x-.5 being positive is most intriguing and not what I would have expected. I don't know how reliable the following speed up it, but at least it's what I would expect. Do[Length[Select[Flatten[data-.5], Positive]],{10}]//Timing {48.06 Second,Null} Do[Length[Select[Flatten[data]-.5, Positive]],{10}]//Timing {42.13 Second,Null} Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 Carl Woll <carlw at u.washington.edu> wrote in message news:7gfu2e$6je at smc.vnet.net... > Hi Allan, > > Your results were somewhat surprising to me, since I didn't expect the Select > option to be the quickest. So, I played around with it, and I came up with an > idea using Count that was faster. Witness > > In[17]:= > data= Table[Random[],{280},{280}]; > Count[data, _?(#>.5&),{2}]//Timing > Count[data, x_/;x>.5,{2}]//Timing > Length[Select[Flatten[data], #1>.5&]]//Timing > Count[data-.5,_?Positive,{2}]//Timing > > Out[17]= > {14.36 Second,39012} > > Out[18]= > {12.84 Second,39012} > > Out[19]= > {11.03 Second,39012} > > Out[20]= > {8.71 Second,39012} > > So, changing the data and using a simpler test allows Count to beat Select. > However, this isn't fair to Select. So doing the same thing for Select yields > > In[21]:= > Length[Select[Flatten[data-.5], Positive]]//Timing > > Out[21]= > {5.29 Second,39012} > > I guess the message here is that Length[Select[...]] is just faster than > Count[...]. > > Carl > > Allan Hayes wrote: > > > Bill > > > > Count needs a pattern; and in this case a level specification. But pattern > > matching is slower than the flattening and testing (with Select) that you > > have used. Here are some timings. > > > > data= Table[Random[],{280},{280}]; > > > > Count[data, _?(#>.5&),{2}]//Timing > > > > {6.86 Second,39219} > > > > Count[data, x_/;x>.5,{2}]//Timing > > > > {6.26 Second,39219} > > > > Length[Select[Flatten[data], #1>.5&]]//Timing > > > > {5.82 Second,39219} > > > > Allan > > > > --------------------- > > Allan Hayes > > Mathematica Training and Consulting > > Leicester UK > > www.haystack.demon.co.uk > > hay at haystack.demon.co.uk > > Voice: +44 (0)116 271 4198 > > Fax: +44 (0)870 164 0565 > > > > W.W. Sampson <w.sampson at umist.ac.uk> wrote in message > > news:7g0qv1$drj at smc.vnet.net... > > > I wish to count the number of elements within a list above > > > a given value. I can do this using the following: > > > > > > Length[Select[Flatten[listname], value > #1&]] > > > > > > However, my list is a 280 x 280 matrix and consequently > > > this takes a while to evaluate. My guess is that there > > > must be something more efficient. I've tried: > > > > > > Count[listname, value > #1&] > > > > > > but the output given is 0, which I know to be incorrect. > > > > > > Any ideas appreciated. > > > > > > Bill > > > > > > > -- > Carl Woll > Dept of Physics > U of Washington > > > >