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

Reply via email to