Re: Using Select within Map
- To: mathgroup at smc.vnet.net
- Subject: [mg71784] Re: [mg71758] Using Select within Map
- From: János <janos.lobb at yale.edu>
- Date: Wed, 29 Nov 2006 02:56:25 -0500 (EST)
- References: <200611281104.GAA27919@smc.vnet.net>
On Nov 28, 2006, at 6:04 AM, 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
Here is a newbie way with Table
In[6]:=
a = 2; b = 8;
In[7]:=
lst = Table[Table[
{Random[Integer, {1, 9}],
Random[Integer,
{1, 9}]}, {i, 3}],
{j, 3}]
Out[7]=
{{{6, 2}, {5, 3}, {9, 8}},
{{9, 8}, {6, 4}, {2, 9}},
{{2, 1}, {4, 6}, {4, 7}}}
In[8]:=
Table[Select[lst[[i]],
a < #1[[1]] < b & ],
{i, 3}]
Out[8]=
{{{6, 2}, {5, 3}}, {{6, 4}},
{{4, 6}, {4, 7}}}
János
----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)
- References:
- Using Select within Map
- From: Mark Teagarden <Mark.Teagarden@utsa.edu>
- Using Select within Map