Re: Using Select within Map
- To: mathgroup at smc.vnet.net
- Subject: [mg71792] Re: Using Select within Map
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 29 Nov 2006 02:56:34 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- 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]
Map[Select[#, (a < #[[1]] < b) &] &, x]
should do what you are looking for.
>
> 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
>
In[1]:=
x = Table[Random[Integer, 10], {3}, {3}, {2}]
Out[1]=
{{{1,5},{8,6},{0,4}},{{2,6},{4,9},{0,4}},{{5,6},{0,9},{10,4}}}
In[2]:=
With[{a = 2, b = 8},
(Select[#1, a < #1[[1]] < b & ] & ) /@ x]
Out[2]=
{{},{{4,9}},{{5,6}}}
In[3]:=
With[{a = 2, b = 8},
(Select[#1, a < #1[[1]] < b & ] & ) /@ x]
Out[3]=
{{},{{4,9}},{{5,6}}}
Regards,
Jean-Marc