[Numpy-discussion] Confusing selector?

Charles R Harris charlesr.harris at gmail.com
Thu Feb 15 12:28:59 EST 2007


On 2/15/07, Geoffrey Zhu <gzhu at peak6.com> wrote:
>
> I am really new to numpy but I just found that v[2:4] means selecting
> v[2] and v[3]. V[4] is not included! This is quite different from the
> conventions of matlab and R.


Yes, it is the Python slicing convention and rather similar to for loops in
C where

for(i = 0; i < N; i++)

is a common construction. It is an consequence of zero based indexing.
Matlab started in Fortran and uses the conventions of the Fortran do loop,
which no doubt makes sense for arrays whose base index is 1. Slicing in
NumPy, and now Python, also includes a step, so you can write a[0:n:2] to
reference all the elements with even indices.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070215/2a1e872f/attachment.html>


More information about the NumPy-Discussion mailing list