[Numpy-discussion] can int and float exists in one array?(about difference in indexing Matlab matrix and Numpy array)
Keith Goodman
kwgoodman at gmail.com
Mon Nov 27 12:13:16 EST 2006
On 11/27/06, Robert Kern <robert.kern at gmail.com> wrote:
> Zhang Sam wrote:
> > 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.
>
> y[x[:,0].astype(int),0]
If you want it exactly like matlab, you'll have to check to make sure that
(x[:,0] == x[:,0].astype(int)).all()
since matlab gives an error if, for example, x[0,0] is 1.00001.
More information about the NumPy-Discussion
mailing list