[Numpy-discussion] numarray.where confusion

Perry Greenfield perry at stsci.edu
Wed May 26 08:25:14 EDT 2004


Alok Singhal wrote:
> Hi,
> 
> I am having trouble understanding how exactly "where" works in
> numarray.
> 
> What I am trying to do:
> 
> I am preparing a two-level mask in an array and then assign values to
> the array where both masks are true:
> 
> >>> from numarray import *
> >>> a = arange(10)
> >>> # First mask
> >>> m1 = where(a > 5)
> >>> a[m1]
> array([6, 7, 8, 9])
> >>> # Second mask
> >>> m2 = where(a[m1] < 8)
> >>> a[m1][m2]
> array([6, 7])
> >>> # So far so good
> >>> # Now change some values
> >>> a[m1][m2] = array([10, 20])
> >>> a[m1][m2]
> array([6, 7])
> >>> a
> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> >>> # Didn't  work
> >>> # Let's try a temporary variable
> >>> t = a[m1]
> >>> t[m2]
> array([6, 7])
> >>> t[m2] = array([10, 20])
> >>> t[m2], t
> (array([10, 20]), array([10, 20,  8,  9]))
> >>> a
> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> 
> So, my assignment to a[m1][m2] seems to work (no messages), but it
> doesn't produce the effect I want it to.
> 
> I have read the documentation but I couldn't find something that would
> explain this behavior.
> 
> So my questions:
> 
> - did I miss something important in the documentation,
> - I am expecting something I shouldn't, or
> - there is a bug in numarray?
> 
(due to confusions with "a" in text I'll use x in place of "a")
I believe the problem you are seeing (I'm not 100% certain yet)
is that although it is possible to assign to an array-indexed
array, that doing that twice over doesn't work since Python is,
in effect, treating x[m1] as an expression even though it is
on the left side. That expression results in a new array that the
second indexing updates, but then is thrown away since it is not
assigned to anything else.

Your second try creates a temporary t which is also not a view into
a so when you update t, a is not updated.

try 

x[m1[0][m2]] = array([10,20])

instead. The intent here is to provide x with the net index array
by indexing m1 first rather than indexing x first.
(note the odd use of m1[0]; this is necessary since where() will
return a tuple of index arrays (to allow use in multidimensional 
cases as indices, so the m1[0] extracts the array from the tuple;
Since m1 is a tuple, indexing it with another index array (well, 
tuple containing an index array) doesn't work).

Perry Greenfield




More information about the NumPy-Discussion mailing list