Numeric: 'where' function conditions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jorl Shefner

    #1

    Numeric: 'where' function conditions

    Could anyone tell me the efficient way to do this? Extracting values
    from an array for a single condition (say all values greater than 'x')
    using 'where' and 'compress' is simple enough.
    [color=blue][color=green][color=darkred]
    >>> from Numeric import arange,where,co mpress
    >>> data= arange(10)
    >>> data= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> mask= where((data > 3),1,0)
    >>> result= compress(mask,d ata)
    >>> result[/color][/color][/color]
    array([4, 5, 6, 7, 8, 9])

    But, 'where' doesn't appear to allow for multiple conditions in one
    statement. For instance, I'd like to do something like:

    mask= where((3 < data <= 7),1,0)

    but, this won't work.

    So, the best I could come up with was this more complicated process that
    requires 2 separate masks and an extra temporary array.
    [color=blue][color=green][color=darkred]
    >>> mask1= where((data > 3),data,0)
    >>> mask1[/color][/color][/color]
    array([0, 0, 0, 0, 4, 5, 6, 7, 8, 9])[color=blue][color=green][color=darkred]
    >>> mask2= where((mask1<= 7),mask1,0)
    >>> mask2[/color][/color][/color]
    array([0, 0, 0, 0, 4, 5, 6, 7, 0, 0])[color=blue][color=green][color=darkred]
    >>> r= compress(mask2, data)
    >>> r[/color][/color][/color]
    array([4, 5, 6, 7])

    Is there a more concise way?

    Thanks,

    J.S.
  • Robert Kern

    #2
    Re: Numeric: 'where' function conditions

    Jorl Shefner wrote:[color=blue]
    > Could anyone tell me the efficient way to do this? Extracting values
    > from an array for a single condition (say all values greater than 'x')
    > using 'where' and 'compress' is simple enough.
    >
    >[color=green][color=darkred]
    >>>>from Numeric import arange,where,co mpress
    >>>>data= arange(10)
    >>>>data= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>>>mask= where((data > 3),1,0)
    >>>>result= compress(mask,d ata)
    >>>>result[/color][/color]
    >
    > array([4, 5, 6, 7, 8, 9])
    >
    > But, 'where' doesn't appear to allow for multiple conditions in one
    > statement. For instance, I'd like to do something like:
    >
    > mask= where((3 < data <= 7),1,0)
    >
    > but, this won't work.[/color]

    Right. "3 < data" creates an array of 0s and 1s where the condition is
    false and true, respectively. You don't need where() at all.

    Try

    mask = logical_and(3 < data, data <= 7)

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Jorl Shefner

      #3
      Re: Numeric: 'where' function conditions

      In article <cogan6$kt0$1@n ews1.ucsd.edu>, Robert Kern <rkern@ucsd.edu >
      wrote:
      [color=blue]
      >
      > Right. "3 < data" creates an array of 0s and 1s where the condition is
      > false and true, respectively. You don't need where() at all.
      >
      > Try
      >
      > mask = logical_and(3 < data, data <= 7)[/color]

      Great. That's exactly what I needed. Thanks.

      Comment

      Working...