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$
  • 4
    $\begingroup$ Count[#, x_ /; x >= Max[#]/2] & /@ a? $\endgroup$ – J. M. will be back soon Feb 19 '16 at 16:05
  • $\begingroup$ counter[sublist_] := Length[Select[sublist, (# >= Max[sublist]/2) &]]; counter /@ a $\endgroup$ – Conor Cosnett Feb 19 '16 at 16:35
  • $\begingroup$ Perfect! Thanks a lot! $\endgroup$ – MicheleG Feb 19 '16 at 17:03
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$
  • $\begingroup$ Thanks for clearing and setteling. I appreciate your A&Q's very much! $\endgroup$ – user9660 Feb 19 '16 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, privacy policy and cookie policy

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