[SciPy-user] Concatenating 1D arrays

Ryan Krauss ryanlists at gmail.com
Thu Feb 9 05:17:34 CST 2006


I am testing this on an old version of scipy on my wife's computer,
but at least in version 0.3.2, you are correct:

In [3]: v1=arange(1,6,2)

In [4]: v1
Out[4]: [1,3,5,]

In [5]: shape(v1)
Out[5]: (3,)

In [6]: v2=arange(2,7,2)

In [7]: v2
Out[7]: [2,4,6,]

In [8]: shape(v2)
Out[8]: (3,)

In [10]: hstack([v1,v2])
Out[10]: [1,3,5,2,4,6,]

There has to be a better solution, but I have a function I wrote
called colwise that basically calls atleast_2d first and then check
the number of rows and columns and transposes if necessary to that the
number of rows is greater than the number of columns.  This isn't real
robust but it works for most situations for me:

In [13]: from rwkmisc import colwise

In [14]: hstack([colwise(v1),colwise(v2)])
Out[14]:
[[1,2,]
 [3,4,]
 [5,6,]]

Here is the function if it would help:

def colwise(matin,makecopy=1):
    if makecopy:
        tempmat=copy.deepcopy(matin)
    else:
        tempmat=matin
    matout=scipy.atleast_2d(tempmat)
    myshape=scipy.shape(matout)
    if myshape[0]<myshape[1]:
        matout=scipy.transpose(matout)
    return matout

Ryan

On 2/9/06, Rudolph van der Merwe <rudolphv at gmail.com> wrote:
> On 2/8/06, Robert Kern <robert.kern at gmail.com> wrote:
> > 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)
>
> The problem (as far as I know) is that all vectors (1-D arrays) are
> treated as row vectors in Numpy. So the example for y will work, but
> I'm not sure it will work for x. How do you construct a 1-D column
> vector which can then be appended to a list and finally hstacked?
>
> R.
>
> --
> Rudolph van der Merwe
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>



More information about the SciPy-user mailing list