[Numpy-discussion] indexing question

Faheem Mitha faheem at email.unc.edu
Sat Mar 27 20:48:14 EST 2004


On Sat, 27 Mar 2004, Todd Miller wrote:

> On Sat, 2004-03-27 at 19:27, Faheem Mitha wrote:
> > On Sat, 27 Mar 2004, Todd Miller wrote:
> >
> > > Try foo[[0,2]].  Note that the array indexing syntax is a little picky:
> > > foo[(0,2)] (tuple index) won't work,  just foo[[0,2]] (list index) or
> > > foo[array([0,2])] (array index).
> >
> > Thanks. I see this extracts the relevant rows. How about columns?
>
> I think for columns you need to transpose the matrix or use take() with
> the right axis=1.

I see. Thanks. I think that take() does what I need. BTW, this
documentation example for take looks wrong, and gives an error for me.

*************************************************
>>> a1 = array([10,20,30,40])
>>> print a1[[3,5]]
[40 60]
>>> print take(a1,[3,5])
[40 60]
**************************************************

I get

>>> a1 = numarray.array([10,20,30,40])
>>> a1
array([10, 20, 30, 40])
>>> numarray.take(a1,[3,5])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1776, in
take
    return array._take((indices,), outarr=outarr, clipmode=clipmode)
  File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line
498, in _take
    return ufunc._take(self, indices, **keywds)
  File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1579, in
__call__
    result = self._doit(computation_mode, woutarr, cfunc, ufargs, 0)
  File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1551, in
_doit
    apply(cfunc, ufargs)
IndexError: Index[1,0]=5 out of range[4]

>>> a1[[3,5]]
array([40, 40])

                                                               Faheem.




More information about the NumPy-Discussion mailing list