Re: logical indexing using more than one variable in a pure function
- To: mathgroup at smc.vnet.net
- Subject: [mg53362] Re: logical indexing using more than one variable in a pure function
- From: "Ray Koopman" <koopman at sfu.ca>
- Date: Sat, 8 Jan 2005 02:39:29 -0500 (EST)
- Organization: http://groups.google.com
- References: <crkue3$akd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Ben Barrowes wrote: > I am trying to pick out values from a matrix using a logical mask, i.e. > using logical indexing. > > I wish the following: > x = {2, 3, 4, 5, 6} > y = {True, False, False, True, True} > x[[y]] > would produce: > {2,5,6} > [...] There are several ways to do it. Here are three: In[1]:= x = {2, 3, 4, 5, 6} Out[1]= {2,3,4,5,6} In[2]:= y = {True, False, False, True, True} Out[2]= {True,False,False,True,True} In[3]:= First/@Select[Transpose@{x,y},#[[2]]&] Out[3]= {2,5,6} In[4]:= Select[Transpose@{x,y},#[[2]]&][[All,1]] Out[4]= {2,5,6} In[5]:= Select[MapThread[If[#2,#1]&,{x,y}],NumberQ] Out[5]= {2,5,6}