MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Counting list elements above/below a given value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17340] Re: [mg17271] Re: Counting list elements above/below a given value
  • From: Carl Woll <carlw at u.washington.edu>
  • Date: Fri, 30 Apr 1999 23:22:49 -0400
  • Organization: Physics Department, U of Washington
  • References: <7g0qv1$drj@smc.vnet.net> <199904300634.CAA21396@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

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





  • Prev by Date: Re: Ugly expressions. Was: Toghether, Apart ?
  • Next by Date: Re: INFORMATIONS
  • Previous by thread: Re: Counting list elements above/below a given value
  • Next by thread: Re:Re: Counting list elements above/below a given value