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.
Answer
Pick[a,#>=0&@@@a]
{{5, -1, 3, -2}, {2, -1, 5, 5}}
Comments
Post a Comment