[SciPy-User] Array indexing question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Array indexing question

Slater Joseph C , PhD, PE
On http://wiki.scipy.org/NumPy_for_Matlab_Users, under Linear Algebra Equivalents, the following row is given:
a(1:5,:)     a[0:5] or a[:5] or a[0:5,:]      the first five rows of a

I'm quite confused as I thought since the first row is indexed as zero, the fifth would be as 5. Indeed, a[5,5] provides the value in the 6th row and 6th column. However, it seems that 0:5 means 0, 1, 2, 3, 4 . So, when used with a colon, the 5 no longer means the same value as when used without. Am I missing something? Why this peculiar behavior, and how does one avoid errors with this inconsistency? (What's the logic to help me understand why it works this way?)

Thank you for any guidance.
Joe




_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Array indexing question

Sturla Molden-3
"Joseph C Slater, PhD, PE" <[hidden email]> wrote:

> On http://wiki.scipy.org/NumPy_for_Matlab_Users, under Linear Algebra
> Equivalents, the following row is given:
> a(1:5,:)     a[0:5] or a[:5] or a[0:5,:]      the first five rows of a
>
> I'm quite confused as I thought since the first row is indexed as zero,
> the fifth would be as 5. Indeed, a[5,5] provides the value in the 6th row
> and 6th column. However, it seems that 0:5 means 0, 1, 2, 3, 4 . So, when
> used with a colon, the 5 no longer means the same value as when used
> without. Am I missing something? Why this peculiar behavior, and how does
> one avoid errors with this inconsistency? (What's the logic to help me
> understand why it works this way?)


Consider what range(5) does. Python lists also index in the same way.

In C we have this too: for(i=0; i<5; i++)

The logic is this: To get n elements starting from i, we take a[i:i+n]. We
also have the nice symmetry a[:n] and a[n:]. BDFL Guido van Rossum decided
that this is how Python objects should index. So that's what NumPy does
too.


Sturla

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Array indexing question

Pauli Virtanen-3
01.04.2015, 22:20, Sturla Molden kirjoitti:
[clip]
> BDFL Guido van Rossum decided
> that this is how Python objects should index. So that's what NumPy does
> too.

http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Array indexing question

jkhilmer@chemistry.montana.edu
In reply to this post by Sturla Molden-3

> Indeed, a[5,5] provides the value in the 6th row
> and 6th column. However, it seems that 0:5 means 0, 1, 2, 3, 4 . So, when
> used with a colon, the 5 no longer means the same value as when used
> without.
 
Consider what range(5) does. Python lists also index in the same way.


From  https://docs.python.org/2/tutorial/introduction.html:
"One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:"
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

If you omit the colon, it's equivalent to a single character/item/row:  a[n] = a[n:n+1]
Jonathan


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Array indexing question

Hanno Klemm

> On 01 Apr 2015, at 22:07, [hidden email] wrote:
>
>
> > Indeed, a[5,5] provides the value in the 6th row
> > and 6th column. However, it seems that 0:5 means 0, 1, 2, 3, 4 . So, when
> > used with a colon, the 5 no longer means the same value as when used
> > without.
>  
> Consider what range(5) does. Python lists also index in the same way.
>
>
> From  https://docs.python.org/2/tutorial/introduction.html:
> "One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:"
>  +---+---+---+---+---+---+
>  | P | y | t | h | o | n |
>  +---+---+---+---+---+---+
>  0   1   2   3   4   5   6
> -6  -5  -4  -3  -2  -1
>
>
> If you omit the colon, it's equivalent to a single character/item/row:  a[n] = a[n:n+1]
> Jonathan
>


This is not entirely correct. the first form tends to yield the element, the second form a sequence:

In [1]: a=[1,2,3]

In [2]: a[1]
Out[2]: 2

In [3]: a[1:2]
Out[3]: [2]

In [5]: import numpy as np

In [6]: a = np.array([1,2,3])

In [7]: a[1]
Out[7]: 2

In [8]: a[1:2]
Out[8]: array([2])



Hanno

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user