Re: Selecting Many Things Rather Than Selecting One Thing From Many
- To: mathgroup at smc.vnet.net
- Subject: [mg66214] Re: [mg66180] Selecting Many Things Rather Than Selecting One Thing From Many
- From: János <janos.lobb at yale.edu>
- Date: Thu, 4 May 2006 05:20:52 -0400 (EDT)
- References: <200605030644.CAA12779@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On May 3, 2006, at 2:44 AM, Gregory Lypny wrote: > Hello everyone, > > I've discovered another use or need for the Select function, which I > suspect requires mapping of some sort. > > In my previous posts, members of this MathGroup kindly showed me how > to apply Select to many columns of a matrix at once. For example, > > (Select[#1, #1 > K & ] & ) /@ Transpose[theMatrix] > > will pull out all values greater than K, where K is a number such as > 100. > > But suppose now that K is a list of numbers, such as K={34, 876, > 199}, and I simply want to extract or identify all of the rows in the > first column of theMatrix equal to any one of those numbers. How > would I do that? I started with > > Select[theMatrix, #[[1]]==any element of list K] > > and I imagine something similar could be applied to the Position > function. > > Any hint would be much appreciated. > > Gregory Here is a newbie approach: In[1]:= n = 8; m = 15; In[2]:= lst = Table[Table[Random[ Integer, {1, 9}], {j, m}], {i, n}] Out[2]= {{8, 8, 9, 4, 7, 7, 6, 9, 1, 1, 6, 3, 4, 9, 3}, {2, 9, 8, 5, 4, 8, 4, 2, 8, 3, 4, 4, 1, 4, 3}, {8, 4, 2, 6, 7, 4, 5, 1, 8, 7, 8, 2, 3, 6, 3}, {4, 4, 5, 4, 2, 5, 9, 7, 2, 4, 5, 4, 4, 7, 9}, {6, 2, 8, 5, 9, 9, 6, 8, 2, 6, 9, 9, 5, 6, 2}, {7, 7, 7, 2, 8, 1, 6, 1, 7, 9, 8, 8, 4, 8, 8}, {7, 1, 4, 6, 8, 4, 4, 1, 5, 7, 1, 8, 3, 3, 5}, {6, 9, 6, 4, 7, 1, 2, 2, 9, 7, 3, 4, 6, 2, 4}} In[3]:= kst = {2, 4, 6} Out[3]= {2, 4, 6} In[4]:= Select[lst, MemberQ[kst, #1[[1]]] & ] Out[4]= {{2, 9, 8, 5, 4, 8, 4, 2, 8, 3, 4, 4, 1, 4, 3}, {4, 4, 5, 4, 2, 5, 9, 7, 2, 4, 5, 4, 4, 7, 9}, {6, 2, 8, 5, 9, 9, 6, 8, 2, 6, 9, 9, 5, 6, 2}, {6, 9, 6, 4, 7, 1, 2, 2, 9, 7, 3, 4, 6, 2, 4}} János ---------------------------------------------- Trying to argue with a politician is like lifting up the head of a corpse. (S. Lem: His Master Voice)
- References:
- Selecting Many Things Rather Than Selecting One Thing From Many
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Selecting Many Things Rather Than Selecting One Thing From Many