Re: select a range of elements from a nested list

*To*: mathgroup at smc.vnet.net*Subject*: [mg5437] Re: select a range of elements from a nested list*From*: "Brian J. Albright" <albright at physics.ucla.edu>*Date*: Sat, 7 Dec 1996 00:26:12 -0500*Organization*: UCLA Department of Physics and Astronomy*Sender*: owner-wri-mathgroup at wolfram.com

Oliver Schurr wrote: > > Hi MathGroup > > Say you have a nested list like that {{x1,y1},{x2,y2},{x3,y3},...{xn,yn}} > basically containing X and Y values for a XY plot in each element. The X > values span a range from say 0 to 0.4 and you ONLY want to select the > elements (x,y values) from the list for which the x value spans say 0 to > 0.3. BUT you don't want to COUNT the elements (visually on the screen) and > figure out the position at which the x value is just greater than 0.3 and > then subtract 1 from this position. > > I still want do define the list by using > > Table[list[[x,1]],list[[x,2]],{x, 1, ZZZ}] > > here ZZZ means the number for which the x value <= 0.3, this number needs > to be found by MATHEMATICA! > > I like to know if there is a general procedure to select elements from a > nested list based on the values of one member of the elements of that > nested list. > > I hope I made myself clear enough to you all. > > Thanks a lot. > > Oliver Schurr > Department of Chemistry > Georgetown University > Washington, DC 20057 Hi Oliver. Ick. Don't build your new list element-by-element. Instead, use the Cases function. E.g.: Input: Output: This is much more elegant and executes faster than building your list one element at a time. Additionally, it works on unsorted data. If for some twisted reason you want to stick with the Table format, you can do the following: Input: 0.3 ] // Length} ] Output: Timings: Input: Output: Input: /; x <= 0.3 ] // Length} ]; ] Output: I hope this helps. -Brian -------------------------------------------|---------------------------- Brian J. Albright | Department of Physics and Astronomy, UCLA | Um, yeah. Uh.... Oh gee. albright at physics.ucla.edu | http://bohm.physics.ucla.edu/~albright | [.signature stage fright] -------------------------------------------|----------------------------