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: [mg53354] Re: [mg53342] logical indexing using more than one variable in a pure function
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sat, 8 Jan 2005 02:39:21 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

x={2,3,4,5,6};

x1={6,5,4,3,2};

Thread[x>3]

{False,False,True,True,True}

#>3&/@x

{False,False,True,True,True}

Thread[x>x1]

{False,False,False,True,True}

Inner[Greater,x,x1,List]

{False,False,False,True,True}

Inner[#1>#2&,x,x1,List]

{False,False,False,True,True}

Pick[x, Thread[x>x1]]

{5,6}

x[[Flatten[Position[Thread[x>x1],
        True]]]]

{5,6}

First/@Cases[Transpose[{x,x1}], 
    x_?(#[[1]]>#[[2]]&)]

{5,6}

Cases[Transpose[{x,x1}], 
  x_?(#[[1]]>#[[2]]&):>x[[1]]]

{5,6}

First/@Select[Transpose[{x,x1}],
    #[[1]]>#[[2]]&]

{5,6}


Bob Hanlon

> 
> From: Ben Barrowes <barrowes at alum.mit.edu>
To: mathgroup at smc.vnet.net
> Date: 2005/01/06 Thu PM 10:00:43 EST
> Subject: [mg53354] [mg53342] logical indexing using more than one variable in a pure 
function
> 
> 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}
> but instead it produces:
> \!\(\*
>    RowBox[{\(Part::"pspec"\), \(\(:\)\(\ \)\), "\<\"Part specification \
> \\!\\({True, False, False, True, True}\\) is neither an integer nor a 
> list of \
> integers. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \
> ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \
> ButtonData:>\\\"General::pspec\\\"]\\)\"\>"}]\)
> {2, 3, 4, 5, 6}\[LeftDoubleBracket]{True, False, False, True,
>      True}\[RightDoubleBracket]
> 
> 
> Here are some working examples when the mask depends upon only one 
variable:
> 
> pick out values in x > 4
> Select[x, (#1 > 4 &)]
> {5, 6}
> 
> BooleanSelect seems to do what I want if I have a logical mask already:
> << Statistics`DataManipulation`
> BooleanSelect[x, y]
> {2, 5, 6}
> And now in 5.1, "Pick" does the same thing as BooleanSelect.
> But how can I do this in one line? (no need to define y explicitly) 
> Something like:
> Pick[x,x>3]
> 
> 
> 
> This seems to work for cases:
> Cases[x, x_ /; x > 3]
> {4, 5, 6}
> and similarly for select:
> Select[x, # > 3 &]
> {4, 5, 6}
> 
> 
> But if I have a second List:
> x1 = {6, 5, 4, 3, 2}
> 
> How would I index x based on a logical operation on both x and x1?
> I hoped something like this would work:
> Pick[x,x>x1]
> or
> Cases[x, {x_,x1_} /; x>x1]
> {}
> But only x is provided to the pattern match, not {x_,x1_}.
> 
> Or maybe a better Select statement. But how do I make the pure function 
> accept two arguments?
> 
> For that matter, how can I perform an element by element logical 
> operation on a list? For example, I would like:
> x>3
> to return
> {False,False,True,True,True}
> instead of
> {2, 3, 4, 5, 6} > 3
> and
> x>x1
> to return
> {False,False,False,True,True}
> instead of
> {2, 3, 4, 5, 6} > {6, 5, 4, 3, 2}
> will something like:
> #1 > #2 &, x, x1
> work?
> 
> I would appreciate any suggestions,
> Ben
> 
> 


  • Prev by Date: Re: Re: FullSimplify with Assumptions
  • 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