Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Probably easy and short question, I still didn't fully figure out how to easily select/delete sublists from a list. Example:

tt = {{2, 4}, {4, 8}}

I want to delete/select all the elements where the difference between them (Abs) is greater than four.

share|improve this question
add comment

5 Answers

up vote 11 down vote accepted

I would use either

Cases[data, {x_, y_} /; Abs[x - y] > 4]

or

With[{diff = Abs[data[[All, 1]] - data[[All, 2]]] - 4}, 
   Pick[data, UnitStep[diff]*Unitize[diff], 1]
]

The first clearly demonstrates what you are trying to do, the second is much faster...

data = RandomInteger[{0, 100}, {10^6, 2}];

(m1 =  Cases[data, {x_, y_} /; Abs[x - y] > 4]); // AbsoluteTiming

==> {2.8393092, Null}

(m2 = 
    With[{diff = Abs[data[[All, 1]] - data[[All, 2]]] - 4}, 
     Pick[data,UnitStep[diff]*Unitize[diff], 1]]); // AbsoluteTiming

==> {0.1248024, Null}

m1 == m2

==> True

Assuming you are working with random data and don't expect equality, the second method can be simplified to...

Pick[data, UnitStep[Abs[data[[All, 1]] - data[[All, 2]]] - 4], 1]

Edit:

Of course if you want to delete elements where Abs[x-y] > 4 you can modify the Cases definition as

Cases[data, {x_, y_} /; Abs[x - y] <= 4]

And the Pick method by swapping out the 1 in the last argument with 0.

share|improve this answer
1  
Since UnitStep[0]==1, you could use Pick[data, UnitStep[4 - Abs[data[[All, 1]] - data[[All, 2]]]], 0] if you want the absolute value to be strictly larger than 4. –  Heike Mar 1 '12 at 17:27
add comment

You can select all sublists the elements of which differ by more than 3 as follows:

Select[tt, Abs[#[[1]] - #[[2]]] > 3 &]
(*{{4, 8}}*)

or using conditional patterns:

Cases[tt, x_ /; Abs[x[[1]] - x[[2]]] > 3]

(and you can delete them by either selecting those Not satisfying the condition, or using DeleteCases).

You could also do

Scan[If[Abs[#[[1]] - #[[2]]] > 3, Sow[#]] &, tt] // Reap // 
  Last // Last

or even

MapThread[If[Abs[#1 - #2] > 3, Sow[{#1, #2}]] &, Transpose@tt]; // 
   Reap // Last // Last

Finally using Compile and Internal`Bag, as described by Andy here, you can do

cs = Compile[{{lst, _Integer, 2}},
   Module[{bag = Internal`Bag[], l = Length@lst},
    Do[
     If[
      Abs[lst[[i, 1]] - lst[[i, 2]]] > 3.,
      Internal`StuffBag[
       bag,
       lst[[i]], 1
       ]
      ],
     {i, l}
     ];
    Partition[Internal`BagPart[bag, All], 2]],
   CompilationTarget -> "C",
   RuntimeOptions -> "Speed"
   ];

which is roughly as fast as the Pick approach described in another answer (neither unpacks).

share|improve this answer
add comment
Cases[tt, {a_, b_} /; Abs[a - b] >= 4]
 {{4, 8}}

or

DeleteCases[tt, {a_, b_} /; Abs[a - b] < 4]
 {{4, 8}}

The latter approach could be better if pairs Abs[a-b] > 4 are generic.

share|improve this answer
add comment

I would also choose Pick for speed, but I would formulate it a little differently:

Pick[
  tt,
  UnitStep[Abs[# - #2] & @@ Transpose[tt] - 5],
  1
]

To pick the complement change the third argument from 1 to 0.

share|improve this answer
add comment

Although not as fast as Pick, another possibility is to use GatherBy which allows selecting/deleting subsets of data based on a condition:

  data = {{2, 4}, {4, 8}, {5, 9}, {1, 3}};
  {deleted,selected}= GatherBy[data, UnitStep[Abs[#[[1]] - #[[2]]] - 4] &]

returns:

   {{{2, 4}, {1, 3}}, {{4, 8}, {5, 9}}}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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