Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

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}


  • Prev by Date: Re: precission/accuracy in mathlink for excel
  • Next by Date: Re: logical indexing using more than one variable in a pure function
  • Previous by thread: Re: logical indexing using more than one variable in a pure function
  • Next by thread: Re: logical indexing using more than one variable in a pure function