[Numpy-discussion] can int and float exists in one array?(about difference in indexing Matlab matrix and Numpy array)

Zhang Sam hollowspook at gmail.com
Mon Nov 27 07:59:41 CST 2006


Hi, there

I  have a practical problem. For some reason, I hope int and float can exist
one array as shown in matlab code.
---------------------------------------------------------------------------
 >> x = [2 2.5 3.5; 1 2.6 3.5]

x =

    2.0000    2.5000    3.5000
    1.0000    2.6000    3.5000

>> y = x(:,2)

y =

    2.5000
    2.6000

>> y(x(:,1))

ans =

    2.6000
    2.5000
------------------------------------------------------------------------------------

However in python with numpy, the similar code is as follows.
------------------------------------------------------------------------------------------
x = array([[2, 2.5, 3.5],[1, 2.6, 3.5]])
>>> x
array([[ 2. ,  2.5,  3.5],
       [ 1. ,  2.6,  3.5]])
>>> y = x[:,1]
>>> y
array([ 2.5,  2.6])
>>> y.shape=2,1
>>> y
array([[ 2.5],
       [ 2.6]])
>>> y[x[:,0],0]
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
IndexError: arrays used as indices must be of integer (or boolean) type
-----------------------------------------------------------------------------------------------------

MATLAB can treat the 1.0,2.00,......as int 1 2,.....
How to realize this matlab code in python? Maybe it exist a simple way.
Please show me .


Thanks in advance

Sam



BTW: I sent this mail to numpy-discussion at lists.sourceforge.net at first,
then I receive a mail which suggests me here. If it has already email that
here, I am very sorry about that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061127/458bc032/attachment.html 


More information about the Numpy-discussion mailing list