<div dir="ltr">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.</div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Feb 26, 2014 at 12:15 AM, Julian Taylor <span dir="ltr"><<a href="mailto:jtaylor.debian@googlemail.com" target="_blank">jtaylor.debian@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On <a href="tel:26.02.2014%2000" value="+12602201400">26.02.2014 00</a>:04, JB wrote:<br>
> At the risk of igniting a flame war...can someone please help me understand<br>
> the indexing behavior of NumPy? I will readily I admit I come from a Matlab<br>
> background, but I appreciate the power of Python and am trying to learn more.<br>
><br>
>>From a Matlab user's perspective, the behavior of indexing in NumPy seems<br>
> very bizarre. For example, if I define an array:<br>
><br>
> x = np.array([1,2,3,4,5,6,7,8,9,10])<br>
><br>
> If I want the first 5 elements, what do I do? Well, I say to myself, Python<br>
> is zero-based, whereas Matlab is one-based, so if I want the values 1 - 5,<br>
> then I want to index 0 - 4. So I type: x[0:4]<br>
><br>
> And get in return: array([1, 2, 3, 4]). So I got the first value of my<br>
> array, but I did not get the 5th value of the array. So the "start" index<br>
> needs to be zero-based, but the "end" index needs to be one-based. Or to put<br>
> it another way, if I type x[4] and x[0:4], the 4 means different things<br>
> depending on which set of brackets you're looking at!<br>
><br>
> It's hard for me to see this as anything by extremely confusing. Can someone<br>
> explain this more clearly. Feel free to post links if you'd like. I know<br>
> this has been discussed ad nauseam online; I just haven't found any of the<br>
> explanations satisfactory (or sufficiently clear, at any rate).<br>
><br>
><br>
<br>
</div>numpy indexing is like conventional C indexing beginning from inclusive<br>
0 to exclusive upper bound: [0, 5[. So the selection length is upper<br>
bound - lower bound.<br>
as a for loop:<br>
for (i = 0; i < 5; i++)<br>
select(i);<br>
<br>
This is the same way Python treats slices.<br>
<br>
in comparison one based indexing is usually inclusive 1 to inclusive<br>
upper bound: [1, 4]. So the selection length is upper bound - lower<br>
bound + 1.<br>
for (i = 1; i <= 4; i++)<br>
select(i);<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br></div>