How to construct matrix from vectors?

Nasser M. Abbasi nma at 12000.org
Sun Jun 21 01:21:34 EDT 2015


On 6/20/2015 10:47 PM, Nasser M. Abbasi wrote:

> I did manage to find a way:
>
> ---------------------------------
> r1 =np.hstack([(v1,v2)]).T
> r2 =np.hstack([(v3,v4)]).T
> mat = np.vstack((r1,r2))
> -----------------------------
>
> Out[211]:
> array([[ 1,  4],
>          [ 2,  5],
>          [ 3,  6],
>          [ 7, 10],
>          [ 8, 11],
>          [ 9, 12]])
>
> But it is not as intuitive as with Matlab, where one can just write
>
> -------------------------------
>     v1=[1,2,3]'; v2=[4,5,6]';
>     v3=[7,8,9]'; v4=[10,11,12]';
>     m=[v1 v2;v3 v4]
> -------------------------------

Here is a way a little closer to Matlab's method: First
make all the vectors column vectors

v1=np.array([(1,2,3)]).T
v2=np.array([(4,5,6)]).T
v3=np.array([(7,8,9)]).T
v4=np.array([(10,11,12)]).T

mat =np.hstack(( np.vstack((v1,v3)), np.vstack((v2,v4))) )

Out[236]:
array([[ 1,  4],
        [ 2,  5],
        [ 3,  6],
        [ 7, 10],
        [ 8, 11],
        [ 9, 12]])

There are way too many '(([[]]))' things  in Python :)





More information about the Python-list mailing list