Services & Resources / Wolfram Forums
MathGroup Archive
*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

logical indexing using more than one variable in a pure function

  • To: mathgroup at
  • Subject: [mg53342] logical indexing using more than one variable in a pure function
  • From: Ben Barrowes <barrowes at>
  • Date: Thu, 6 Jan 2005 22:00:43 -0500 (EST)
  • Sender: owner-wri-mathgroup at

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}
would produce:
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, \
{2, 3, 4, 5, 6}\[LeftDoubleBracket]{True, False, False, True,

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:

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:
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:
to return
instead of
{2, 3, 4, 5, 6} > 3
to return
instead of
{2, 3, 4, 5, 6} > {6, 5, 4, 3, 2}
will something like:
#1 > #2 &, x, x1

I would appreciate any suggestions,

  • Prev by Date: Re: global assumptions?? How far can I go?
  • Next by Date: Re: Converting between Spherical and Cartesian coordinates
  • Previous by thread: Re: Mathematica and Visual basic
  • Next by thread: Re: logical indexing using more than one variable in a pure function