[Numpy-discussion] Negative indexing.

Benjamin Root ben.root@ou....
Mon Jan 16 15:05:17 CST 2012


On Sun, Jan 15, 2012 at 6:54 AM, David Cournapeau <cournape@gmail.com>wrote:

> On Sat, Jan 14, 2012 at 11:53 PM, Nathan Faggian
> <nathan.faggian@gmail.com> wrote:
> > Hi,
> >
> > I am finding it less than useful to have the negative index wrapping on
> nd-arrays. Here is a short example:
> >
> > import numpy as np
> > a = np.zeros((3, 3))
> > a[:,2] = 1000
> > print a[0,-1]
> > print a[0,-1]
> > print a[-1,-1]
> >
> > In all cases 1000 is printed out.
> >
> > What I am after is a way to say "please don't wrap around" and have
> negative indices behave in a way I choose.  I know this is a standard thing
> - but is there a way to override that behaviour that doesn't involve cython
> or rolling my own resampler?
>
> Although it could be possible with lots of work, it would most likely
> be a bad idea. You will need to wrap something around your
> model/data/etc... Could you explain a bit more what you have in mind ?
>
> David
>

Another approach that might be useful, depending on the needs, is to use
`np.ravel_multi_index()`, in which ndim coords can be passed in and flatten
coords are returned.  It has options of 'raise', 'wrap' and 'clip' for
handling out-of-bounds indices.  It wouldn't be built directly into the
arrays, but if that isn't needed, this might work.

Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120116/e9cce650/attachment.html 


More information about the NumPy-Discussion mailing list