Re: Counting list elements above/below a given value
- To: mathgroup at smc.vnet.net
- Subject: [mg17279] Re: [mg17202] Counting list elements above/below a given value
- From: BobHanlon at aol.com
- Date: Fri, 30 Apr 1999 23:22:19 -0400
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 4/26/99 6:44:09 AM, w.sampson at umist.ac.uk writes: >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, Your test is backwards to find elements above a given value; however, your initial approach is about the quickest of a few that I have tried. value = 0.5; n = 280; listname= Table[Random[], {i, n}, {j, n}]; Length[Select[Flatten[listname], #>value&]]//Timing {2.8 Second,39224} Length[Cases[Flatten[listname], x_?(#>value&)]]//Timing {3.63333 Second,39224} Plus @@ Cases[Flatten[listname], x_?(#>value&) -> 1]//Timing {3.71667 Second,39224} Count[Flatten[listname], x_?(#>value&)]//Timing {3.51667 Second,39224} Length[Cases[Flatten[listname]-value, _?Positive]]//Timing {3.31667 Second,39224} Length[Select[Flatten[listname]-value, Positive[#]&]]//Timing {3.3 Second,39224} Length[Flatten[Select[#-value, Positive[#]&]& /@ listname]]//Timing {3.41667 Second,39224} Length[Flatten[Cases[#-value, _?Positive]& /@ listname]]//Timing {3.31667 Second,39224} Bob Hanlon