2
$\begingroup$

I have data points of the form {x,y,z}:

s = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}

And I need to remove entire data points based on a condition. For example, let's say that I want to remove any data points where z > 6. The result should be this:

s2 = {{0, 1, 2}, {3, 4, 5}}

How do I do this? I think DeleteCases might be the way to go, but I'm still fairly inexperienced with Mathematica and am not sure how to use this function to make this work.

$\endgroup$
  • $\begingroup$ Have you seen Select? $\endgroup$ – Edmund Nov 8 '17 at 21:43
4
$\begingroup$
s = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
s2 = Pick[s, Thread[s[[All, 3]] > 6], False]

or

s2 = Pick[s, Thread[s[[All, 3]] <= 6]]

{{0, 1, 2}, {3, 4, 5}}

$\endgroup$
  • $\begingroup$ Can this be used to set upper and lower bounds for a coordinate in one go? $\endgroup$ – whatwhatwhat Nov 8 '17 at 21:41
  • 3
    $\begingroup$ @whatwhatwhat You can write Thread[4 <= s[[All, 3]] <= 6] $\endgroup$ – Coolwater Nov 8 '17 at 21:55
  • 1
    $\begingroup$ @what, you might also be interested in the function Between[]. $\endgroup$ – J. M. will be back soon Nov 9 '17 at 0:15
4
$\begingroup$

DeleteCases works perfectly here, although I think Select is a bit more natural

DeleteCases[s, {_, _, _?(# > 6 &)}]
Select[s, Last@# <= 6 &]

(* {{0, 1, 2}, {3, 4, 5}} *)
(* {{0, 1, 2}, {3, 4, 5}} *)
$\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.