How to construct matrix from vectors?

Nasser M. Abbasi nma at 12000.org
Sat Jun 20 23:47:26 EDT 2015


On 6/20/2015 9:20 PM, MRAB wrote:

> Here's one way, one step at a time:
>
>>>> r1 = np.concatenate([v1, v2])
>>>> r1
> array([1, 2, 3, 4, 5, 6])
>>>> r2 = np.concatenate([v3, v4])
>>>> r2
> array([ 7,  8,  9, 10, 11, 12])
>>>> m = np.array([r1, r2])
>>>> m
> array([[ 1,  2,  3,  4,  5,  6],
>          [ 7,  8,  9, 10, 11, 12]])
>>>> m.transpose()
> array([[ 1,  7],
>          [ 2,  8],
>          [ 3,  9],
>          [ 4, 10],
>          [ 5, 11],
>          [ 6, 12]])
>>>>
>

But your output is wrong.

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]
-------------------------------

--Nasser






More information about the Python-list mailing list