Re: select a range of elements from a nested list
- To: mathgroup at smc.vnet.net
- Subject: [mg5427] Re: select a range of elements from a nested list
- From: haberndt at dnai.com (Harald Berndt)
- Date: Sat, 7 Dec 1996 00:25:59 -0500
- Organization: DNAI ( Direct Network Access )
- Sender: owner-wri-mathgroup at wolfram.com
In article <57ush1$3cb at dragonfly.wolfram.com>, Oliver Schurr
<schurro at gusun.acc.georgetown.edu> 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.
There is, and it's quite simple. A quick look into the documentation turns
up the function Select[], which even sounds like the right thing to try.
So, let's say you have a list
In[14]:=
t1
Out[14]=
{{0.,0.00498942},{0.02,0.00637976},{0.04,0.00853591},{0.06,0.00774966},{0.08,
0.0146887},{0.1,0.0159799},{0.12,0.010505},{0.14,0.0218018},{0.16,
0.0158617},{0.18,0.0377771},{0.2,0.036134},{0.22,0.0565887},{0.24,
0.0579106},{0.26,0.0711923},{0.28,0.0787898},{0.3,0.0801005},{0.32,
0.0984567},{0.34,0.123357},{0.36,0.137414},{0.38,0.139577},{0.4,0.152203}}
Simply select all pairs whose first part has the right properties:
In[15]:=
Select[t1, First[#]<=0.3&]
Out[15]=
{{0.,0.00498942},{0.02,0.00637976},{0.04,0.00853591},{0.06,0.00774966},{0.08,
0.0146887},{0.1,0.0159799},{0.12,0.010505},{0.14,0.0218018},{0.16,
0.0158617},{0.18,0.0377771},{0.2,0.036134},{0.22,0.0565887},{0.24,
0.0579106},{0.26,0.0711923},{0.28,0.0787898},{0.3,0.0801005}}
It doesn't even matter whether the list is ordered or not. It's reaonably
fast, too ...
>
> I hope I made myself clear enough to you all.
>
Well, and I hope I got it.
Best,
--
Harald Berndt, Ph.D. Research Specialist,
Voice: 510-652-5974 Consultant
FAX: 510-215-4299