4
$\begingroup$

I want removing elements of a table. For example, I have the next table:

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

and I want removing all elements where the 1st element of sublist is between 6 and 2 for I obtain this new table:

data = {{0, 3}, {1, 3}, {6, 8}, {7, 4}};

The next code don´t work:

DeleteCases[data, Flatten[Select[data, 6 > #[[1]] > 2 &]]]

If only I want removing one element (for example {0,3}), the code work:

DeleteCases[data, Flatten[Select[data, 1 > #[[1]] >= 0 &]]]

How can I remove elements where the 1st element of sublist is between two values?.

$\endgroup$
7
  • $\begingroup$ There has got to be a dupe for this, but in the meantime: why not stick to Select? E.g. Select[data, Not[6 > #[[1]] > 2] &]. $\endgroup$ Aug 10, 2016 at 9:46
  • $\begingroup$ DeleteCases[data,x/;(x[[1]]>xmin&&x[[1]]<xmax)] where xmin and xmax are your thresholds for first element $\endgroup$
    – Rom38
    Aug 10, 2016 at 9:47
  • $\begingroup$ mathematica.stackexchange.com/q/2486/5478 $\endgroup$
    – Kuba
    Aug 10, 2016 at 9:54
  • 1
    $\begingroup$ The second argument of DeleteCases must be a pattern. I recommend reading through some of these chapters: reference.wolfram.com/language/tutorial/PatternsOverview.html $\endgroup$
    – Szabolcs
    Aug 10, 2016 at 9:54
  • 1
    $\begingroup$ Why {6,8} is left but {2,5} is not. $\endgroup$
    – Kuba
    Aug 10, 2016 at 9:55

2 Answers 2

4
$\begingroup$

You can pick the desired elements, e.g.

Pick[data, 2 <= #[[1]] < 6 & /@ data, False]
Pick[data, Not[2 <= #[[1]] < 6] & /@ data]

or deleting:

DeleteCases[data, {_?(2 <= # < 6 &), _}]
$\endgroup$
3
  • $\begingroup$ When I import table of a file with two columns (x,y) I have data = {{{0, 3}, {1, 3}, {2, 5}, {4, 2}, {5, 1}, {6, 8}, {7, 4}}}. In these case only work the @MarkusDichtl code. I don´t understand why. $\endgroup$
    – Manu
    Aug 10, 2016 at 11:50
  • $\begingroup$ @Manu I am sorry I do not understand your comment. In my timezone I am off to sleep. I will aim to look after I get up but I am certain other uses will resolve :) $\endgroup$
    – ubpdqn
    Aug 10, 2016 at 11:53
  • $\begingroup$ @Manu. In that case, replace data in ubpdqn's code with First@data, and it should work. $\endgroup$
    – march
    Aug 10, 2016 at 16:03
4
$\begingroup$

New approach since v10.2:

data /. {x_ /; 6 > x >= 2, y_} -> Nothing

and Nothing just vanishes, resulting in

{{0, 3}, {1, 3}, {6, 8}, {7, 4}}
$\endgroup$
2
  • 2
    $\begingroup$ In the version 10 work too. $\endgroup$
    – Manu
    Aug 10, 2016 at 11:22
  • $\begingroup$ Nothing was introduced in 10.2, it's a special symbol for the standard evaluation process. $\endgroup$
    – BoLe
    Aug 10, 2016 at 11:35

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.