Re: [Numpy-discussion] Help Understanding Indexing Behavior
JB, This behavior is a property of python slicing. It takes some getting used to, but has its advantages. In general in a slice [i:j] the indices go from i to j-1. In the case that i is 0 it's easy to think of it as j giving the number of elements (by the way you can also do a[:j] -- i.e. leaving out the 0 -- and get the same result. Maybe someone else could provide more background on why slicing is defined the way it is in python, but in the end you just have to get used to it. Jon On Tue, Feb 25, 2014 at 6:07 PM, numpy-discussion-requ...@scipy.org wrote: From: JB jonathan.j.b...@gmail.com To: numpy-discussion@scipy.org Cc: Date: Tue, 25 Feb 2014 23:04:26 + (UTC) Subject: [Numpy-discussion] Help Understanding Indexing Behavior At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] And get in return: array([1, 2, 3, 4]). So I got the first value of my array, but I did not get the 5th value of the array. So the start index needs to be zero-based, but the end index needs to be one-based. Or to put it another way, if I type x[4] and x[0:4], the 4 means different things depending on which set of brackets you're looking at! It's hard for me to see this as anything by extremely confusing. Can someone explain this more clearly. Feel free to post links if you'd like. I know this has been discussed ad nauseam online; I just haven't found any of the explanations satisfactory (or sufficiently clear, at any rate). -- Jonathan D. Slavin Harvard-Smithsonian CfA jsla...@cfa.harvard.edu 60 Garden Street, MS 83 phone: (617) 496-7981 Cambridge, MA 02138-1516 fax: (617) 496-7577USA ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
JB jonathan.j.b...@gmail.com wrote: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? x[:5] ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
On Wed, Feb 26, 2014 at 5:32 AM, Slavin, Jonathan jsla...@cfa.harvard.eduwrote: This behavior is a property of python slicing. It takes some getting used to, but has its advantages. quite a few, actually! They key with slicing is to think of the index as pointing to the space between the elements: 0 1 2 3 4 5 | | | | | | but the reason (and beauty) of this is that it results in a number of nifty properties: len( seq [i:j] ) == j-i seq[i:j] + seq[j:k] == seq[i:k] len( seq[:i] ) == i len( seq[-i:] ) == i and if you have an array representing a axis, for instance, and want to know the value of a given index: x = x_0 + i*dx or the index of a given value: i = (x - x_0) / dx Notice that I don't have a single +1 or -1 in all of that -- this make sit easier to undersand and a lot less error prone. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Help Understanding Indexing Behavior
At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] And get in return: array([1, 2, 3, 4]). So I got the first value of my array, but I did not get the 5th value of the array. So the start index needs to be zero-based, but the end index needs to be one-based. Or to put it another way, if I type x[4] and x[0:4], the 4 means different things depending on which set of brackets you're looking at! It's hard for me to see this as anything by extremely confusing. Can someone explain this more clearly. Feel free to post links if you'd like. I know this has been discussed ad nauseam online; I just haven't found any of the explanations satisfactory (or sufficiently clear, at any rate). ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
On 26.02.2014 00:04, JB wrote: At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] And get in return: array([1, 2, 3, 4]). So I got the first value of my array, but I did not get the 5th value of the array. So the start index needs to be zero-based, but the end index needs to be one-based. Or to put it another way, if I type x[4] and x[0:4], the 4 means different things depending on which set of brackets you're looking at! It's hard for me to see this as anything by extremely confusing. Can someone explain this more clearly. Feel free to post links if you'd like. I know this has been discussed ad nauseam online; I just haven't found any of the explanations satisfactory (or sufficiently clear, at any rate). numpy indexing is like conventional C indexing beginning from inclusive 0 to exclusive upper bound: [0, 5[. So the selection length is upper bound - lower bound. as a for loop: for (i = 0; i 5; i++) select(i); This is the same way Python treats slices. in comparison one based indexing is usually inclusive 1 to inclusive upper bound: [1, 4]. So the selection length is upper bound - lower bound + 1. for (i = 1; i = 4; i++) select(i); ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
Think of the python indices as the edges of the boxes, whereas the matlab indices are the boxes themselves. matlab: [1][2][3][4] python: 0[ ]1[ ]2[ ]3[ ]4[ ]5 you need to do 0:5 in python or you won't contain all the boxes! On 25 February 2014 23:04, JB jonathan.j.b...@gmail.com wrote: At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] And get in return: array([1, 2, 3, 4]). So I got the first value of my array, but I did not get the 5th value of the array. So the start index needs to be zero-based, but the end index needs to be one-based. Or to put it another way, if I type x[4] and x[0:4], the 4 means different things depending on which set of brackets you're looking at! It's hard for me to see this as anything by extremely confusing. Can someone explain this more clearly. Feel free to post links if you'd like. I know this has been discussed ad nauseam online; I just haven't found any of the explanations satisfactory (or sufficiently clear, at any rate). ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
To elaborate on what Julian wrote: it is indeed simply a convention; slices/ranges in python are from the start to one-past-the-end. The reason for the emergence of this convention is that C code using iterators looks most natural this way. This manifests in a simple for (i = 0; i 5; i++), but also when specifying a slice of a linked list, for instance. We don't want to terminate the loop when we are just arriving at the last item; we want to terminate a loop when we have gone past the last item. Also, the length of a range is simply end-start under this convention; no breaking your head over -1 or +1. Such little nudges of elegance pop up all over C code; and that's where the convention comes from. Same as zero-based indexing; just a convention, and if you are going to pick a convention you might as well pick one that minimizes the number of required operations. Anything but zero-based indexing will require additional integer math to find an array element, given its base pointer. On Wed, Feb 26, 2014 at 12:15 AM, Julian Taylor jtaylor.deb...@googlemail.com wrote: On 26.02.2014 00:04, JB wrote: At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] And get in return: array([1, 2, 3, 4]). So I got the first value of my array, but I did not get the 5th value of the array. So the start index needs to be zero-based, but the end index needs to be one-based. Or to put it another way, if I type x[4] and x[0:4], the 4 means different things depending on which set of brackets you're looking at! It's hard for me to see this as anything by extremely confusing. Can someone explain this more clearly. Feel free to post links if you'd like. I know this has been discussed ad nauseam online; I just haven't found any of the explanations satisfactory (or sufficiently clear, at any rate). numpy indexing is like conventional C indexing beginning from inclusive 0 to exclusive upper bound: [0, 5[. So the selection length is upper bound - lower bound. as a for loop: for (i = 0; i 5; i++) select(i); This is the same way Python treats slices. in comparison one based indexing is usually inclusive 1 to inclusive upper bound: [1, 4]. So the selection length is upper bound - lower bound + 1. for (i = 1; i = 4; i++) select(i); ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
On 26/02/2014 00:04, JB wrote: At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] The Python slicing syntax a:b defines the interval [a, b), while the Matlab syntax defines the interval [a:b]. This post from Guido van Rossum (the creator of Python) explains the choice of zero indexing and of this particular slice notation: https://plus.google.com/115212051037621986145/posts/YTUxbXYZyfi I actually find how Python works more straight forward: obtaining the first n elements of array x is simply x[:n], and obtaining n elements starting at index i is x[i:i+n]. Cheers, Daniele ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Help Understanding Indexing Behavior
On Tue, Feb 25, 2014 at 6:01 PM, Daniele Nicolodi dani...@grinta.netwrote: On 26/02/2014 00:04, JB wrote: At the risk of igniting a flame war...can someone please help me understand the indexing behavior of NumPy? I will readily I admit I come from a Matlab background, but I appreciate the power of Python and am trying to learn more. From a Matlab user's perspective, the behavior of indexing in NumPy seems very bizarre. For example, if I define an array: x = np.array([1,2,3,4,5,6,7,8,9,10]) If I want the first 5 elements, what do I do? Well, I say to myself, Python is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5, then I want to index 0 - 4. So I type: x[0:4] The Python slicing syntax a:b defines the interval [a, b), while the Matlab syntax defines the interval [a:b]. This post from Guido van Rossum (the creator of Python) explains the choice of zero indexing and of this particular slice notation: https://plus.google.com/115212051037621986145/posts/YTUxbXYZyfi I actually find how Python works more straight forward: obtaining the first n elements of array x is simply x[:n], and obtaining n elements starting at index i is x[i:i+n]. To enlarge just a bit, as said, python indexing comes from C, Matlab indexing comes from Fortran/Matrix conventions. If you look at how Fortran compiles, it translates to zero based under the hood, starting with a pointer to memory one location before the actual array data, so C just got rid of that little wart. Chuck ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion