MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using Select within Map

  • To: mathgroup at smc.vnet.net
  • Subject: [mg71803] Re: Using Select within Map
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Wed, 29 Nov 2006 02:56:54 -0500 (EST)
  • References: <ekh70s$s7p$1@smc.vnet.net>

Mark Teagarden wrote:
> Hi,
>
> I have a data set which comprises several paired lists, like so (I apologize
> if the tabs do not come out correctly in your mail client):
>
> x =
> {
>     {
>         { , },
>         { , },
>         { , }
>     },
>     {
>         { , },
>         { , },
>         { , }
>     },
>     {
>         { , },
>         { , },
>         { , }
>     }
> }
>
> What I would like to do is to select from each of those paired lists, only
> those pairs where the first value in the pair falls within some specified
> range, and then obtain a mean.  This would be simple enough if I was
> operating on a single paired list, for example:
>
> Select[x[[1]],a < #[[1]] < b&]
>
> However, I would like to Map over x so that I would end up with means for
> each of the level 1 lists within x; therein lies the problem.  Both Map and
> Select use the Slot operator (#), and I don't know how to distinguish
> between the slot operator used by Map, and the slot operator used by Select:
>
> Select[#,a < #[[1]] < b&]/@x
>
> Or if you prefer:
>
> Map[Select[#, a < #[[1]] < b&,x]
>
> This problem has been plaguing me for some time, and if I could punch
> through it I would be very happy indeed.  Any ideas?  I have had no luck
> looking through the archives or the Help Browser.  On a similar note, I
> would eventually like to modify this solution so that the criteria, a and b,
> could vary with each level 1 list in x, but one thing at a time...
>
> Thanks in advance,
> Mark
>
> --
> Mark A. Teagarden, Ph.D.
> University of Texas at San Antonio
> Department of Biology
> One UTSA Circle
> San Antonio, TX 78249

In[1]:= x = {{{1,2},{3,4},{5,6}},
             {{7,8},{9,0},{0,9}},
             {{8,7},{6,5},{4,3}}};

Your code for the first list in x works nicely:

In[2]:= With[{a = 2, b = 6}, Select[x[[1]], a < #[[1]] < b &]]
Out[2]= {{3,4},{5,6}}

Your code for the whole x needs another ampersand, after the Select:

In[3]:= With[{a = 2, b = 6}, Select[#, a < #[[1]] < b &]& /@ x]
Out[3]= {{{3,4},{5,6}},{},{{4,3}}}

If a and b are not constant, but have values specified in a list ab,
write one of the functions in the form that uses named parameters:

In[4]:= With[{ab = {{2,5},{3,6},{4,7}}},
MapThread[Function[{x,ab}, Select[x, ab[[1]] < #[[1]] < ab[[2]] &]],
          {x,ab}]]
Out[4]= {{{3,4}},{},{{6,5}}}


  • Prev by Date: Re: Using Select within Map
  • Next by Date: Graphics--How to plot all functions issued from For loop and color
  • Previous by thread: Re: Using Select within Map
  • Next by thread: Re: Using Select within Map