views:

158

answers:

3

I have used NumPy for my Master thesis. I've converted parts of the code from MATLAB code, but I have doubts in NumPy/Python when I reference:

m = numpy.ones((10,2))
m[:,0]

which returns:

array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

and when I ref to:

m[:,0:1]

it returns:

array([[ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.]])

that I think it should be, cause same result with MATLAB!!!

A: 

Hi

I forget what numpy does, but Matlab indexes vectors from 1, not 0. So array(:,0) is an error in Matlab.

Regards

Mark

High Performance Mark
+3  A: 

I'm still learning Python myself, but I think the way that slicing works is that indices point to in-between locations, therefore 0:1 only gets you the first column. Is this what you were asking about?

This is what the documentation has to say:

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:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1
Amro
No m[:,0:1] is the equivalent of the matlab statement as shown in the question - this extracts column 0, equivalent to column 1 in Matlab. 0:2 is a slice covering, 0,1 which is both columns (so the whole array in this case)
thrope
I think this what the OP was trying to do, since (aside from the starting index) in MATLAB: `m(:,1:2)` gets you two columns, while in Python/NumPy: `m[:,0:1]` returns one column. I guess this is something to be clarified..
Amro
Agree its not clear - I read the question as asking about extracting a column - in matlab m(:,1) gives a 2d column vector - the same as m[:,0:1] in numpy, but m[:,0] in numpy gives a 1d array - I think the question was why this is different to matlab, but the slices translate as you say...
thrope
Yea, it's not definitely clear in the question. I meant the same as Amro: MATLAB m(:,1:2), NumPy m[:,0:2]
rcs
+4  A: 

This is because numpy has the concept of 1d arrays which Matlab doesn't have. Coupled with numpys broadcasting this provides a powerful simplification (less worrying about inserting transposes everywhere) but does mean you have to think a little bit about translating from Matlab. In this case, extracting a single column with a scalar Numpy simplifies the result to a 1d array - but with a slice it preserves the original dimensions. If you want to stay closer to Matlab semantics you could try using the Matrix class. See NumPy for matlab users page for details. In this case, you could do either of the following:

m[:,0][:,newaxis] # gives same as matlab
np.matrix(m)[:,0] # gives same as matlab

But remember if you use matrix class * becomes matrix multiplication and you need to use multiply() for elementwise. (This is all covered in NumPy for Matlab Users page). Generally I would recommend trying to get used to using 1d arrays where you would have column or row vector in matlab and generally things just work. You only need to worry about column vs row when reassembling them into a 2d array.

You may be interested in automated matlab to python converters such as OMPC (paper) (I think there are others as well).

thrope
ohh, great, i got it :D, thank very much , thrope!!!
vernomcrp
no problem... If you feel this answers the question you could click on the tick by the answer to accept it... :)
thrope