4
$\begingroup$

That's must be a very trivial question. Suppose we have the simple list

data = {{-1, 0}, {0.2, 0.4}, {4, 0}, {0.3, 0}, {0.2, -0.4}};

Then, is there a quick and elegant way to pick the element with non-zero and positive y value? In this example, we should get {0.2, 0.4}.

$\endgroup$
2
  • 3
    $\begingroup$ Select[data, Positive[#[[2]]]&] $\endgroup$
    – Jason B.
    Jun 27 '19 at 14:36
  • $\begingroup$ Alternatively, data//Pick[#, Sign[#[[All,2]]],1]& $\endgroup$
    – user1066
    Jun 27 '19 at 15:50
5
$\begingroup$

Select is a good option here. Jason has shown in the comments how to use the Positive function. If you want a bit more flexibility for future usage, you can use a standard greater than operator.

data = {{-1, 0}, {0.2, 0.4}, {4, 0}, {0.3, 0}, {0.2, -0.4}};    
positivedata=Select[data, #[[2]] > 0 &]
$\endgroup$
4
$\begingroup$

You can also use Cases:

Cases[{_, _?Positive}] @ data

{{0.2, 0.4}}

$\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.