How to construct matrix from vectors?
Dave Farrance
df at see.replyto.invalid
Sun Jun 21 07:49:16 EDT 2015
Fabien <fabien.maussion at gmail.com> wrote:
>another solution with less "(([[]]))", and less ";". There are way too
>many ";" in Matlab ;)
>
>import numpy as np
>v1 = [1, 2, 3]
>v2 = [4, 5, 6]
>v3 = [7, 8, 9]
>v4 = [10, 11, 12]
>np.hstack([[v1, v2], [v3, v4]]).T
>Out[]:
>array([[ 1, 4],
> [ 2, 5],
> [ 3, 6],
> [ 7, 10],
> [ 8, 11],
> [ 9, 12]])
Neat. And if the OP wants "vectors" in np array form to start with, and
to stack them together without transposing at that point, he could do it
like this:
>>> v1=np.vstack([1,2,3])
>>> v2=np.vstack([4,5,6])
>>> v3=np.vstack([7,8,9])
>>> v4=np.vstack([10,11,12])
>>> np.r_[np.c_[v1,v2],np.c_[v3,v4]]
array([[ 1, 4],
[ 2, 5],
[ 3, 6],
[ 7, 10],
[ 8, 11],
[ 9, 12]])
And since he seems to want a Matlab-like environment, then the somewhat
depreciated pylab was intended to dump a Matlab-like set of functions
into the namespace, which is OK for an interactive environment, an not
too much of a problem for a short program in a single module. Probably
best to do that with iPython, though.
>>> from matplotlib.pylab import *
>>> v1=vstack([1,2,3])
>>> v2=vstack([4,5,6])
>>> v3=vstack([7,8,9])
>>> v4=vstack([10,11,12])
>>> r_[c_[v1,v2],c_[v3,v4]]
array([[ 1, 4],
[ 2, 5],
[ 3, 6],
[ 7, 10],
[ 8, 11],
[ 9, 12]])
More information about the Python-list
mailing list