MathGroup Archive 1999

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

Search the Archive

Re: Counting list elements above/below a given value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17266] Re: [mg17202] Counting list elements above/below a given value
  • From: "Tomas Garza" <tgarza at mail.internet.com.mx>
  • Date: Fri, 30 Apr 1999 02:34:47 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

W.W. Sampson [w.sampson at umist.ac.uk] wrote:

> 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.

Hi, Bill!

First of all, if you want to count the number of elements "above" a given
value, you should invert the sense of the < sign in both your expressions
above.

Second, the use of Count requires a pattern (as shown below), which is not
what you have.

Third, either you have a very slow machine or you are exceedingly greedy
with your computer time. As an example, I use a 1000 x 1000 matrix, which is
far greater than the one you have in your problem, and obtain the following
results (suppose value = 500):

In[1]:=
listA=Table[{Random[Integer,{1,1000}],Random[Integer,{1,1000}]},{1000}];
In[2]:=
value=500;
In[3]:=
Length[Select[Flatten[listA], value < #1&]]//Timing
Out[3]=
{0.05 Second,999}
In[4]:=
Count[Flatten[listA],x_/;x>value]//Timing
Out[4]=
{0.06 Second,999}

(observe the use of a pattern together with a condition when using Count).
Now, one twentieth of a second should be good enough, unless you have to
repeat this calculation thousands of times!

I hope this helps.

Tomas Garza
Mexico City



  • Prev by Date: RE: Counting list elements above/below a given value
  • Next by Date: Re: re: my previous question on matrix element assignment
  • Previous by thread: RE: Counting list elements above/below a given value
  • Next by thread: Re: Counting list elements above/below a given value