3
$\begingroup$

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.

$\endgroup$
3
  • 4
    $\begingroup$ Count[#, x_ /; x >= Max[#]/2] & /@ a? $\endgroup$ Feb 19, 2016 at 16:05
  • $\begingroup$ counter[sublist_] := Length[Select[sublist, (# >= Max[sublist]/2) &]]; counter /@ a $\endgroup$
    – Conor
    Feb 19, 2016 at 16:35
  • $\begingroup$ Perfect! Thanks a lot! $\endgroup$
    – MicheleG
    Feb 19, 2016 at 17:03

2 Answers 2

6
$\begingroup$

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}
$\endgroup$
1
  • $\begingroup$ Thanks for clearing and setteling. I appreciate your A&Q's very much! $\endgroup$
    – user9660
    Feb 19, 2016 at 17:33
5
$\begingroup$

Alternatively:

Tr[UnitStep[# - Max[#]/2]] & /@ a
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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