Skip to main content
edited tags
Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
more stuff
Source Link
user170231
  • 1.7k
  • 1
  • 11
  • 17

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.

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.

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.

Source Link
user170231
  • 1.7k
  • 1
  • 11
  • 17

Removing all values from a list that satisfy a given condition

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.