[Numpy-discussion] Matlab -> NumPy translation and indexing

Robert Cimrman cimrman3 at ntc.zcu.cz
Wed Mar 14 09:46:38 EDT 2007


David Koch wrote:
> Hi,
> 
> so one thing I came across now is the following, very simple:
> 
> Matlab:
> A = []
> while ....
>    A = [A some_scalar_value]
> end
> 
> 
> In Python, I tried:
> 
> A = empty((0,0))
> while ....
>    A = concatenate((A, array([someScalarValue])), 1)
> end
> 
> which returns an error since the shape of the empty A does not match the
> vector I want to concatenate with. Any way to get around this without
> having
> to use some obscure "if exist A" clause. I am still stuck in my Matlab ways
> - hoping to get rid of them though.

a = empty( (0,) )
...

I would bet that using a regular Python list would be faster in this
case than growing the numpy array.

a = []
while ...
 a.append( scalar )
a = array( a )

r.



More information about the NumPy-Discussion mailing list