MathGroup Archive 2005

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

Search the Archive

Re: logical indexing using more than one variable in a pure function


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