# [SciPy-User] Array indexing question Classic List Threaded 5 messages Open this post in threaded view
|

## [SciPy-User] Array indexing question

 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
Open this post in threaded view
|

## Re: Array indexing question

 "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
Open this post in threaded view
|

## Re: Array indexing question

 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
 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