Re: Selecting table rows
- To: mathgroup at smc.vnet.net
- Subject: [mg66047] Re: Selecting table rows
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 27 Apr 2006 05:36:01 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e2ppth$t4g$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Skirmantas wrote: > I have a 2D-table whose first column contains the unique identifiers (ID) of each row (IDs can be any numbers). I need to delete all rows whose IDs are not members of another, control list. > > The following works, but I'm wondering if there is a shorter way to do this. > > TBL = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}}; > > CNTR = {1, 9, 17}; > > Map[Part[TBL, #]&, Flatten[Map[Position[Transpose[TBL][[1]], #]&, CNTR]]] > > Thanks -- > > Skirmantas > Hi Skirmantas, You could use the built-in function *Select*. For instance, In[1]:= TBL = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}}; In[2]:= CNTR = {1, 9, 17}; In[3]:= Select[TBL, MemberQ[CNTR, #1[[1]]] & ] Out[3]= {{1, 2, 3, 4}, {9, 10, 11, 12}, {17, 18, 19, 20}} Best regards, Jean-Marc