[Numpy-discussion] Confusing selector?

Charles R Harris charlesr.harris@gmail....
Thu Feb 15 11:28:59 CST 2007

On 2/15/07, Geoffrey Zhu <gzhu@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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070215/2a1e872f/attachment-0001.html 

More information about the Numpy-discussion mailing list