[Numpy-discussion] numarray.where confusion
as8ca at virginia.edu
Wed May 26 10:19:09 CDT 2004
On 26/05/04: 11:24, Perry Greenfield wrote:
> (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.
Thanks or this info. It makes sense now. I suspected earlier that t
was not a view but a copy, but didn't realise that the same thing was
happening with x[m1][m2].
> x[m1[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; this is necessary since where() will
> return a tuple of index arrays (to allow use in multidimensional
> cases as indices, so the m1 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).
This works, but for the fact that in my real code I *am* dealing with
multidimensional arrays. But this is a nice trick to remember.
(So, the following "does not work":
x = arange(9)
m1 = where(x > 4)
m2 = where(x[m1] < 7)
On 26/05/04: 11:41, Todd Miller wrote:
> Here's how I did it (there was an easier way I overlooked):
> a = arange(10)
> m1 = where(a > 5, 1, 0).astype('Bool')
> m2 = where(a < 8, 1, 0).astype('Bool')
> a[m1 & m2] = array([10, 20])
Ah. This works! Even for multidimensional arrays.
On 26/05/04: 18:06, Francesc Alted wrote:
> Perhaps the easier way looks like this?
> >>> a = arange(10)
> >>> a[(a>5) & (a<8)] = array([10, 20])
> >>> a
> array([ 0, 1, 2, 3, 4, 5, 10, 20, 8, 9])
> Indexing is a very powerful (and fun) thing, indeed :)
I like this too. Thank you all for the help!
Alok Singhal (as8ca at virginia.edu) __
Graduate Student, dept. of Astronomy / _
University of Virginia \_O \
More information about the Numpy-discussion