[SciPy-user] Concatenating 1D arrays

aurelian a.u.r.e.l.i.a.n at gmx.net
Wed Feb 8 05:10:22 CST 2006


Hi,

I know two ways of achieving what you want.

The first one, which is the 'closer' translation of the matlab code, reads:

-----
from numpy import zeros, concatenate

x=zeros((vlen, 0))
y=zeros((vlen, 0))
# Use zeros((vlen, 0)) to get an 'empty' column-oriented 2d array.
# vlen is the length of one of your vectors.

for k in xrange(0, N): # this will run from 0 to N-1!
    c = foo(k)
    r = bar(k)
    x = concatenate((x, c), axis=1)
    y = concatenate((y, r), axis=1)
----

The way I prefer is to initialize x and y with full dimension and "fill"
it. This is imho easier to read and shorter.

----
from numpy import zeros

x = zeros((vlen, N), dtype=footype)
y = zeros((vlen, N), dtype=bartype)
# note that x and y must have correct datatype

for k in xrange(0, N):
    # take a slice of x and set it to the return values of foo(k)
    x[:, k] = foo(k)
    y[:, k] = bar(k)
----

HTH,
Johannes Loehnert



More information about the SciPy-user mailing list