Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'resulting': 0.04; 'column': 0.07; 'subject:How': 0.09; 'rows': 0.09; 'python': 0.11; '#no': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'numpy': 0.16; 'received:192.168.1.4': 0.16; 'tried:': 0.16; 'wrote:': 0.16; 'shape': 0.18; 'python?': 0.18; '>>>': 0.20; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'skip:m 30': 0.27; 'time:': 0.27; 'correct': 0.29; 'trouble': 0.31; 'link.': 0.32; 'problem': 0.33; 'subject:?': 0.34; 'to:addr:python-list': 0.35; 'i.e.': 0.35; 'step': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'skip:v 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'some': 0.40; 'easy': 0.60; 'learn': 0.60; 'skip:n 10': 0.63; 'today': 0.64; 'url:htm': 0.73; 'construct': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JOrGyJ+b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=ktWuRk-RAAAA:8 a=pw3dmCtVTJaW6KyP3DoA:9 a=QEXdDO2ut3YA:10 a=ERBVmtYjrAMA:10 X-AUTH: mrabarnett@:2500 Date: Sun, 21 Jun 2015 03:20:24 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to construct matrix from vectors? References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434853238 news.xs4all.nl 2827 [2001:888:2000:d::a6]:55534 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92924 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]]) >>>