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$
1
  • $\begingroup$ Have you seen Select? $\endgroup$
    – Edmund
    Commented Nov 8, 2017 at 21:43

2 Answers 2

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$
3
  • $\begingroup$ Can this be used to set upper and lower bounds for a coordinate in one go? $\endgroup$ Commented Nov 8, 2017 at 21:41
  • 3
    $\begingroup$ @whatwhatwhat You can write Thread[4 <= s[[All, 3]] <= 6] $\endgroup$
    – Coolwater
    Commented Nov 8, 2017 at 21:55
  • 1
    $\begingroup$ @what, you might also be interested in the function Between[]. $\endgroup$ Commented Nov 9, 2017 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 and acknowledge you have read our privacy policy.

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