1
$\begingroup$

I'm looking for a short way to eliminate a list from a matrix, if the first element of the list is negative. I've come up with this:

a = {{5, -1, 3, -2}, {-4, -3, -3, -4}, {2, -1, 5, 5}}

f[x_] := Table[x, 4];
Pick[a, f /@ UnitStep[a[[All, 1]]], 1] /. {} -> Nothing

The output is and should be:

{{5, -1, 3, -2}, {2, -1, 5, 5}}

In my example, all lists have a length of 4, this explains the Table[x, 4]. A general case would be more interesting.

$\endgroup$

closed as off-topic by mikado, xslittlegrass, MarcoB, happy fish, m_goldberg Apr 17 '17 at 15:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – mikado, xslittlegrass, MarcoB, happy fish, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ Select[a,#[[1]] >= 0&] $\endgroup$ – user46676 Apr 16 '17 at 19:11
2
$\begingroup$
Pick[a,#>=0&@@@a]

{{5, -1, 3, -2}, {2, -1, 5, 5}}

$\endgroup$

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