10
$\begingroup$

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.

$\endgroup$
  • 4
    $\begingroup$ 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] $\endgroup$ – Nasser Nov 6 '14 at 3:33
  • $\begingroup$ 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. $\endgroup$ – user170231 Nov 6 '14 at 3:38
  • 2
    $\begingroup$ Please include minimal code for your "circles" example. $\endgroup$ – Mr.Wizard Nov 6 '14 at 3:49
  • $\begingroup$ 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} $\endgroup$ – Nasser Nov 6 '14 at 3:58
  • $\begingroup$ 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. $\endgroup$ – Nasser Nov 6 '14 at 4:08
12
$\begingroup$
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]
$\endgroup$
  • 1
    $\begingroup$ Yep, still fairly new to Mathematica. Thanks for the assist. $\endgroup$ – user170231 Nov 7 '14 at 5:56
5
$\begingroup$
list = Table[{n}, {n, 1, 5}];

Select[list, #[[1]] <= 3 &]
(*{{1}, {2}, {3}}*)
$\endgroup$
4
$\begingroup$

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}
$\endgroup$
3
$\begingroup$
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}*)
$\endgroup$
  • 1
    $\begingroup$ Another one for your Clip set: Flatten @ Clip[rad, {10, 700}, {{}, {}}] $\endgroup$ – Mr.Wizard Nov 6 '14 at 9:41
  • 1
    $\begingroup$ I just noticed that you're mapping UnitStep. You're doing it wrong (case #4). ;^) Replace /@ with @ for a nice performance improvement. $\endgroup$ – Mr.Wizard Nov 6 '14 at 9:54
  • $\begingroup$ @Mr.Wizard thank you...I too often forget which functions are listable. $\endgroup$ – ubpdqn Nov 6 '14 at 10:01

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.