[SciPy-user] Concatenating 1D arrays

Robert Kern robert.kern at gmail.com
Wed Feb 8 11:26:25 CST 2006


Rudolph van der Merwe wrote:
> What is the Numpy prefered way of recursively concatenating 1D arrays
> (vectors) into a 2D array (matrix)?
> 
> I'm trying to convert the following Matlab code to Python:
> 
> x=[];
> y=[];
> for k=1:N,
>     c = get_column_vector_from_somewhere()
>     r = get_row_vector_from_somewhere()
>     x = [x c]
>     y = [y; r]
> end

Assuming that you don't actually know N (in which case Johannes' second example
is appropriate),

x = []
y = []

while have_more_vectors():
    x.append(get_column_vector_from_somewhere())
    y.append(get_row_vector_from_somewhere())

x = hstack(x)
y = vstack(y)

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter



More information about the SciPy-user mailing list