0
$\begingroup$

I have a list and add only the values which are > 0 with:

Total[Cases[mylist, x_ /; x > 0]]

How to do this if I now have a list of sublists?

Map[Total[Cases[???, x_ /; x > 0]], mylistoflists]

I tried to use # (to reference the actual sublist) which doesn't work. I tried ## (to reference all values of the actual sublist) which doesn't work.

So, how can I reference the actual list while iterating over all sublists?

$\endgroup$
  • $\begingroup$ What exactly have you tried with #? You might want to read reference.wolfram.com/language/tutorial/PureFunctions.html $\endgroup$ – Szabolcs Jul 9 '18 at 17:26
  • 1
    $\begingroup$ It is possible that I do not understand your question. Please give a sample input and the corresponding desired output. E.g. here's an input: mylistoflists = {{-1, 2, -3, 4}, {-4, 3, -2, 1}}. I'll wait with writing an answer until you respond. $\endgroup$ – Szabolcs Jul 9 '18 at 17:29
  • 3
    $\begingroup$ So, what's wrong with Map[Total[Cases[#, x_ /; x > 0]] &, mylistoflists] (notice the &)? Btw., Total[Ramp[mylistoflists], {2}] should be a faster way to accomplish your goals (by a speed factor of 100 or so). $\endgroup$ – Henrik Schumacher Jul 9 '18 at 17:53
  • $\begingroup$ @HenrikSchumacher Thanks! I was missing the & as I thought Mathematica would implicitly a pure function. Ramp looks good too. Is there a function of the same concept for x <= 0? $\endgroup$ – Robert M. Münch Jul 10 '18 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.