[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 19:13:32 CST 2006


Thanks for so many replies.

In fact, I want to use several arrays to store the original data from a
practical project. In every arrays, two or three column will be store the
index. The main computation is still on matrices(float type) which is built
from the original data. When building the main matrix, I need the repeated
use of the index stored in the original data. So I hope both int and float
can exist in one array with numpy, just for the original data.

Before Python, I used matlab and fortran. In matlab, it is just I have said.
In fortran, a module can used for storing different data type. I used Python
just now, so I don't know which one in python is best for my case. What's
the suggestion?

Thans again

BTW: I will try' object' as array type today. I just know this type for my
first time.

2006/11/28, Tim Hochberg <tim.hochberg at ieee.org>:
>
> Zhang Sam wrote:
> > 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 .
>
> What are you trying to do here? Is the first column always going to
> consist of integral values? If so, I'd point you to record arrays. Using
> those you could store the first column (more or less -- you'd actually
> access is slightly differently) as integers and the other two columns as
> floats. Alternatively, you could simply store things as two separate
> arrays, one int and one float. Or is something else going on?
>
> You could store all of your values as python objects, but I would
> recommend against it in almost all cases. Object arrays are cantankerous
> beasts, you will be chewing up a lot of memory unnecessarily and you'll
> loose any opportunities for speeding things up using numpy. It is true
> that, if you are doing the majority of your calculations in standard
> Python, it is faster at least for small arrays, to keep things in object
> arrays. However, if you are doing most of your computation that way, it
> is likely that something is badly wrong in the sense that you are not
> utilizing numpy as it's meant to be used.
>
> -tim
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061128/e1419740/attachment.html 


More information about the Numpy-discussion mailing list