MathGroup Archive 2007

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

Search the Archive

Re: Selecting Rows Where All Columns Satisfy a Condition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82602] Re: Selecting Rows Where All Columns Satisfy a Condition
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Fri, 26 Oct 2007 05:15:12 -0400 (EDT)
  • References: <ffppu6$l6g$1@smc.vnet.net>

Gregory Lypny wrote:
> Hello everyone
> 
> I've got an Nx4 numeric array called X.  I'd like to pull out all  
> rows where -9 does not appear in any of the four columns.  I know I  
> can this:

I do not understand this sentence (please just give an example instead 
of using the ambiguous terms "row" and "column"),

> Select[X, #[[1]] != -9 && #[[2]] != -9 && #[[3]] != -9 && #[[4]] !=  
> -9 &]

however, this Mathematica code fragment suggests that you have the data 
in this format:

x = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, ... },

and would like to select the sublists that do not contain -9.

> But is there a more elegant way that applies the not-equal-to  
> condition to each column without having to repeat it?
> 

In this case, the following should work:

Select[x, FreeQ[#, e_ /; e == -9] &]

Here I used e_ /; e == -9 instead of simply -9 because -9 == -9.0 is 
True, but -9 === -9.0 is False.  This may or may not be a problem in 
your application.

Other alternatives:

x /. {___, -9, ___} -> Sequence[]

Select[x, VectorQ[#, # != -9 &] &]

Select[x, And@@(# != -9 & /@ #) &]

-- 
Szabolcs


  • Prev by Date: Re: (x|y) \[element] Integers in Reduce function
  • Next by Date: Re: Converting notebooks to use Mathematica Player
  • Previous by thread: Re: Selecting Rows Where All Columns Satisfy a Condition
  • Next by thread: binomial sum bug