From: Pau G. <pau...@gm...> - 2006-05-18 15:23:21
|
On 5/18/06, Bill Baxter <wb...@gm...> wrote: > One thing I haven't quite managed to figure out yet, is what the heck > indexing an array with an array is supposed to give you. > This is sort of an offshoot of the "nonzero()" discussion. > I was wondering why nonzero() and where() return tuples at all instead of > just an array, till I tried it. > > >>> b > array([[0, 1, 2], > [3, 4, 5], > [6, 7, 8]]) > >>> b[ num.asarray(num.where(b>4)) ] > array([[[3, 4, 5], > [6, 7, 8], > [6, 7, 8], > [6, 7, 8]], > > [[6, 7, 8], > [0, 1, 2], > [3, 4, 5], > [6, 7, 8]]]) > > Whoa, not sure what that is. Can someone explain the current rule and in > what situations is it useful? > > Thanks > --bb i think that is b and x are arrays then, b[x]_ijk =3D b[ x_ijk ] where ijk is whatever x needs to be indexed with. Note that the indexing on b is _only_ on its first dimension. >>> from numpy import * >>> b =3D arange(9).reshape(3,3) >>> b array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> x,y =3D where(b>4) >>> xy =3D asarray( (x,y) ) >>> xy array([[1, 2, 2, 2], [2, 0, 1, 2]]) >>> b[x,y] array([5, 6, 7, 8]) >>> b[xy] array([[[3, 4, 5], [6, 7, 8], [6, 7, 8], [6, 7, 8]], [[6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]]]) >>> asarray( (b[x],b[y]) ) array([[[3, 4, 5], [6, 7, 8], [6, 7, 8], [6, 7, 8]], [[6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]]]) |