Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92924
| Date | 2015-06-21 03:20 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: How to construct matrix from vectors? |
| References | <mm55le$4sj$1@speranza.aioe.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.665.1434853238.13271.python-list@python.org> (permalink) |
On 2015-06-21 02:57, Nasser M. Abbasi wrote:
> I just started to learn some python today for first time,
> so be easy on me.
>
> I am having some trouble figuring how do the problem shown in this link
>
> http://12000.org/my_notes/mma_matlab_control/KERNEL/KEse44.htm
>
> Given 4 column vectors, v1,v2,v3,v4, each is 3 rows.
>
> I want to use these to construct matrix mat, which is
>
> [[v1,v2],
> [v3,v4]]
>
> So the resulting matrix is as shown in the link. i.e.
> it will be 6 rows and 2 columns.
>
> This is what I tried:
>
> import numpy as np
> v1=np.array([1,2,3]);
> v2=np.array([4,5,6]);
> v3=np.array([7,8,9]);
> v4=np.array([10,11,12]);
>
> And now I get stuck, I tried
>
> m=np.array([[v1,v2],[v3,v4]]) #no good
>
> Also
>
> m=np.array([v1,v2,v3,v4])
> m.shape
> Out[153]: (4L, 3L)
> m.T
>
> array([[ 1, 4, 7, 10],
> [ 2, 5, 8, 11],
> [ 3, 6, 9, 12]])
>
> Not what I want.
>
> I need to get the shape as in the above link, 6 rows by 2 columns,
> where each column vector is stacked as shown. I also tried
>
> v1=np.array([1,2,3]); v1.shape=3,1
> v2=np.array([4,5,6]); v2.shape=3,1
> v3=np.array([7,8,9]); v3.shape=3,1
> v4=np.array([10,11,12]); v4.shape=3,1
> mat=np.array([[v1,v2],[v3,v4]])
>
> What is the correct way to do this in Python?
>
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]])
>>>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to construct matrix from vectors? "Nasser M. Abbasi" <nma@12000.org> - 2015-06-20 20:57 -0500
Re: How to construct matrix from vectors? MRAB <python@mrabarnett.plus.com> - 2015-06-21 03:20 +0100
Re: How to construct matrix from vectors? "Nasser M. Abbasi" <nma@12000.org> - 2015-06-20 22:47 -0500
Re: How to construct matrix from vectors? "Nasser M. Abbasi" <nma@12000.org> - 2015-06-21 00:21 -0500
Re: How to construct matrix from vectors? Fabien <fabien.maussion@gmail.com> - 2015-06-21 10:49 +0200
Re: How to construct matrix from vectors? Dave Farrance <df@see.replyto.invalid> - 2015-06-21 12:49 +0100
Re: How to construct matrix from vectors? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-21 09:32 +0100
Re: How to construct matrix from vectors? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-21 11:12 -0400
Re: How to construct matrix from vectors? lanuradha@gmail.com - 2015-06-21 09:00 -0700
Re: How to construct matrix from vectors? Tony the Tiger <tony@tiger.invalid> - 2015-07-09 20:23 +0000
csiph-web