Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've the following problem: given a nested list like this:

a={{1,2,3,2,1},{2,3,4,6,4,3,2},{4,5,12,5,4}};

How can I count, in each sublist, those values that are following a particular rule, like the values bigger or equal than half the maximum value in the sublist? In this case I'd like a result like this:

b={3,5,1}

Thanks.

share|improve this question
4  
Count[#, x_ /; x >= Max[#]/2] & /@ a? – J. M. Feb 19 at 16:05
    
counter[sublist_] := Length[Select[sublist, (# >= Max[sublist]/2) &]]; counter /@ a – John Conor Cosnett Feb 19 at 16:35
    
Perfect! Thanks a lot! – Michele Guastamacchia Feb 19 at 17:03
up vote 6 down vote accepted

To settle this:

a = {{1, 2, 3, 2, 1}, {2, 3, 4, 6, 4, 3, 2}, {4, 5, 12, 5, 4}};
Count[#, x_ /; x >= Max[#]/2] & /@ a
   {3, 5, 1}
share|improve this answer
    
Thanks for clearing and setteling. I appreciate your A&Q's very much! – Louis Feb 19 at 17:33

Alternatively:

Tr[UnitStep[# - Max[#]/2]] & /@ a
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.