[SciPy-user] Concatenating 1D arrays

Ed Schofield schofield at ftw.at
Wed Feb 8 12:40:36 CST 2006


Rudolph van der Merwe wrote:

>>The way I prefer is to initialize x and y with full dimension and "fill"
>>it. This is imho easier to read and shorter.
>>    
>>
>The problem is that the full dimension, i.e. number of vectors to be
>concatenated is not known apriori. I know that growing arrays in-place
>as apposed to filling a pre-sized array is very inefficient in Matlab
>(is this also the case for Numpy?), but sometimes it cannot be
>avoided.
>  
>
How about a list of arrays?

>>> x = []; y = []
>>> for k in xrange(N):
...     x += [get_col_vec()]
...     y += [get_row_vec()]
...

and then, when you're done, convert to matrices with
>>> x = mat(x).T
>>> y = mat(x)

This is slower than pre-allocating an array, but should be MUCH faster
than the Matlab construct you cited, which I think reallocates memory
and copies the entire matrix each iteration ;)

-- Ed



More information about the SciPy-user mailing list