12

Suppose I have a list,

list = Table[{n}, {n, 1, 5}]
(* list = {{1},{2},{3},{4},{5}} *)

and I want to remove all values from this list that are greater than 3, so that I get

newlist = {{1}, {2}, {3}}

Is there a way to do this without having something cumbersome like

newlist1 = list /. {4} -> Sequence[]
(* newlist1 = {{1}, {2}, {3}, {5}} *)
newlist = newlist1 /. {5} -> Sequence[]
(* newlist = {{1}, {2}, {3}} *)

possibly with an If statement?

Some context: I have a set of circles and a list of each circle's radius, but I want to remove all the circles with radius $r$ outside of a few set ranges. For example, if I have a hundred circles with radii ranging from 1 to 10, I would then like to eliminate all the circles with radii $r<2$, those with $4<r<6$, and those with $r>8$.

Any suggestions are appreciated.

Edit: Here's what I've tried by adapting Nasser's code:

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308,
       27.1662, ... , 80.0562, 799.3, 44.5997, 14.0357}

DeleteCases[rad, {x_} /; {x>10 And x<700}]
(* the 799.3 is an extreme outlier *)

but I apparently don't understand the syntax required for setting multiple constraints. I've tried different variations, like

DeleteCases[rad, {x_,y_} /; {x>3, y<1}]

but again, no luck.

Kuba
  • 136,707
  • 13
  • 279
  • 740
user170231
  • 1,611
  • 1
  • 11
  • 17
  • 4
    DeleteCases[list, x_ /; First@x > 3] If you had made your table like this: Table[n, {n, 1, 5}] it would be easier a little: DeleteCases[list, x_ /; x > 3] – Nasser Nov 06 '14 at 03:33
  • Nice, that works perfectly for the really simple example I posted. How can I modify it for the circle example? And yeah, I had defined list as I did because /. 4->Sequence[] didn't work properly. – user170231 Nov 06 '14 at 03:38
  • 2
    Please include minimal code for your "circles" example. – Mr.Wizard Nov 06 '14 at 03:49
  • In your new code, replace it with this: DeleteCases[rad, x_ /; (x > 10 && x < 700)] then it will work. I get {6.94622, 799.3} – Nasser Nov 06 '14 at 03:58
  • If your list has {{a},{b}....} and not {a,b,c}, then use DeleteCases[rad, x_ /; (First@x > 10 && First@x < 700)] You need First@ to be do x[[1]] basically, since each x is a list now. (you can write x[[1]] if you prefer. – Nasser Nov 06 '14 at 04:08

4 Answers4

14
rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997, 14.0357, 1000};

DeleteCases[rad, x_ /; 10 > x || x > 700]

(* {34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, 14.0357} *)

You need an urgent syntax surgery. (* lol *)

Or using the converse, Cases:

Cases[rad, x_ /; 10 <= x <= 700]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
4

Here's my contribution, making the same assumptions as the other two answers:

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997, 
   14.0357, 1000};

rad ~Pick~ IntervalMemberQ[Interval[{10, 700}], rad]
{34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, 14.0357}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4
list = Table[{n}, {n, 1, 5}];

Select[list, #[[1]] <= 3 &]
(*{{1}, {2}, {3}}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
3
rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997, 14.0357, 1000};

Just for fun (noting would need to vary depending on strictness of inequalities):

Clip[rad, {10, 700}, {"out", "out"}] /. ("out" :> Sequence[])
Flatten @ Clip[rad, {10, 700}, {{}, {}}] (* as per Mr. Wizard*)
DeleteCases[Clip[rad, {10, 700}, {"out", "out"}], "out"]
Pick[rad, UnitStep[# - 10] - UnitStep[# - 700] & @ rad, 1] 
(* latter: improvement as per Mr. Wizard , see comment*)

all yield:

(*{34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, \
14.0357}*)
ubpdqn
  • 60,617
  • 3
  • 59
  • 148