[Numpy-discussion] Confusing selector?
Charles R Harris
Thu Feb 15 11:28:59 CST 2007
On 2/15/07, Geoffrey Zhu <firstname.lastname@example.org> wrote:
> I am really new to numpy but I just found that v[2:4] means selecting
> v and v. V 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
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...
More information about the Numpy-discussion