[Numpy-discussion] Matlab page on scipy wiki

Sebastian Haase haase at msg.ucsf.edu
Thu Mar 2 20:18:03 CST 2006


Hi,
I noted on
http://www.scipy.org/Wiki/NumPy_for_Matlab_Users

matlab  a(1:5,:)
numpy  a[0:4] or a[0:4,:]  a[0:4] or  a[0:4,:]
the first five rows of a

I think this is wrong!

in numpy it would be: a[:5] (or a[0:5] )  for the first five elements

To the best of my knowledge (I have never used Matlab myself!) this is 
one of the biggest points of confusion for Matlab users !!
WHY DOES a[4:6] NOT INCLUDE THE ELEMENTS 4,5 *AND* 6 ???

The only explanation I have is that its
a) like a C/C++-for-loop (for(i=4;i<6;i++)   --> note the '<'  (not '<=')
b) it then also always is true that: "last" minus first is equal to 
number-of-elements (example: 6-4 = 2)
c) it turns out from experience that this convention WILL save you lots 
of '-1' and '+1' in your code (actually almost all of them)
[if I see a "+1" in a Matlab person's numpy code I can almost always 
guess that he/she made a mistake !]

Maybe this paragraph could be added to the wiki ...

Thanks for this wiki page - I think I looks great

- Sebastian Haase




More information about the Numpy-discussion mailing list