[Numpy-discussion] Getting an item in an array with its coordinates given by another array
bernhard.voigt@gmai...
bernhard.voigt@gmai...
Mon Oct 29 03:39:12 CDT 2007
Take a look at numpy.ix_
http://www.scipy.org/Numpy_Example_List_With_Doc#head-603de8bdb62d0412798c45fe1db0648d913c8a9c
This method creates the index array for you. You only have to specify
the coordinates in each dimesion.
Bernhard
On Oct 29, 8:46 am, "Matthieu Brucher" <matthieu.bruc...@gmail.com>
wrote:
> > > Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]],
> > which
> > > is even stranger.
>
> > c[(2,3)] is the same as c[2,3] and obviously works as you expected.
>
> Well, this is not indicated in the documentation.
>
> c[[2,3]] is refered to as 'advanced indexing' in the numpy book.
>
> > It will return elements 2 and 3 along the first dimension. To get what you
> > want, you need to put it like this:
>
> > c[[2],[3]]
>
> > In [118]: c = N.arange(0.,3*4*5).reshape((3,4,5))
>
> > In [119]: c[[2],[3]]
>
> > Out[119]: array([[ 55., 56., 57., 58., 59.]])
>
> This is very strange to say the least, as tuple do not work in the same way.
>
> This does not work however using a ndarray holding the indices.
>
>
>
>
>
> > In [120]: ind = N.array([[2],[3]])
>
> > In [121]: c[ind]
>
> > ---------------------------------------------------------------------------
> > <type 'exceptions.IndexError'> Traceback (most recent call
> > last)
>
> > /media/hda6/home/ck/<ipython console> in <module>()
>
> > <type 'exceptions.IndexError'>: index (3) out of range (0<=index<=2) in
> > dimension 0
>
> > so you have to convert it to a list before:
>
> > In [122]: c[ind.tolist()]
> > Out[122]: array([[ 55., 56., 57., 58., 59.]])
>
> But if I have the coordinates of the points in an array, I have to reshape
> it and then convert it into a list. Or convert it into a list and then
> convert it to a tuple. I know that advanced indexing is useful, but here it
> is not coherent. tuples and lists should have the same result on the array,
> or at least it should be documented.
> So there is not way to get a sub-array based on coordinates in an array ?
>
> Matthieu
>
> --
> French PhD student
> Website :http://miles.developpez.com/
> Blogs :http://matt.eifelle.comandhttp://blog.developpez.com/?blog=92
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
More information about the Numpy-discussion
mailing list