Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92922 > unrolled thread

How to construct matrix from vectors?

Started by"Nasser M. Abbasi" <nma@12000.org>
First post2015-06-20 20:57 -0500
Last post2015-07-09 20:23 +0000
Articles 10 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#92922 — How to construct matrix from vectors?

From"Nasser M. Abbasi" <nma@12000.org>
Date2015-06-20 20:57 -0500
SubjectHow to construct matrix from vectors?
Message-ID<mm55le$4sj$1@speranza.aioe.org>
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?

thanks,
--Nasser

[toc] | [next] | [standalone]


#92924

FromMRAB <python@mrabarnett.plus.com>
Date2015-06-21 03:20 +0100
Message-ID<mailman.665.1434853238.13271.python-list@python.org>
In reply to#92922
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]])
>>>

[toc] | [prev] | [next] | [standalone]


#92928

From"Nasser M. Abbasi" <nma@12000.org>
Date2015-06-20 22:47 -0500
Message-ID<mm5c4c$fmm$1@speranza.aioe.org>
In reply to#92924
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


[toc] | [prev] | [next] | [standalone]


#92930

From"Nasser M. Abbasi" <nma@12000.org>
Date2015-06-21 00:21 -0500
Message-ID<mm5hks$p52$1@speranza.aioe.org>
In reply to#92928
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 :)

[toc] | [prev] | [next] | [standalone]


#92941

FromFabien <fabien.maussion@gmail.com>
Date2015-06-21 10:49 +0200
Message-ID<mm5tq9$gtp$1@speranza.aioe.org>
In reply to#92930
On 06/21/2015 07:21 AM, Nasser M. Abbasi wrote:
> 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 :)

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

[toc] | [prev] | [next] | [standalone]


#92953

FromDave Farrance <df@see.replyto.invalid>
Date2015-06-21 12:49 +0100
Message-ID<n88doa55115nuvmejapjal4cl1adpq1is7@4ax.com>
In reply to#92941
Fabien <fabien.maussion@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]])

[toc] | [prev] | [next] | [standalone]


#92940

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-06-21 09:32 +0100
Message-ID<mailman.669.1434875589.13271.python-list@python.org>
In reply to#92928
On 21/06/2015 04:47, Nasser M. Abbasi wrote:

>
> But it is not as intuitive as with Matlab
>

For those of us who don't know would you be kind enough to do a cost 
comparison of Matlab vs Python licenses?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#92964

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-06-21 11:12 -0400
Message-ID<mailman.679.1434899573.13271.python-list@python.org>
In reply to#92928
On Sun, 21 Jun 2015 09:32:55 +0100, Mark Lawrence <breamoreboy@yahoo.co.uk>
declaimed the following:

>On 21/06/2015 04:47, Nasser M. Abbasi wrote:
>
>>
>> But it is not as intuitive as with Matlab
>>
>
>For those of us who don't know would you be kind enough to do a cost 
>comparison of Matlab vs Python licenses?

	Or even Matlab vs Octave
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#92967

Fromlanuradha@gmail.com
Date2015-06-21 09:00 -0700
Message-ID<29d53e0b-b9ed-4f8c-854e-1a1667021211@googlegroups.com>
In reply to#92964
On Sunday, 21 June 2015 20:43:15 UTC+5:30, Dennis Lee Bieber  wrote:
> On Sun, 21 Jun 2015 09:32:55 +0100, Mark Lawrence declaimed the following:
> 
> >On 21/06/2015 04:47, Nasser M. Abbasi wrote:
> >
> >>
> >> But it is not as intuitive as with Matlab
> >>
> >
> >For those of us who don't know would you be kind enough to do a cost 
> >comparison of Matlab vs Python licenses?
> 
> 	Or even Matlab vs Octave

Well if its a parentheses minimization contest, APL will beat the pants of everything.

Heres a web-session at http://baruchel.hd.free.fr/apps/apl/

   v1 ← 1 2 3
   v2 ← 4 5 6
   v3 ← 7 8 9
   v4 ← 10 11 12
   v1
1 2 3

# We need to reshape (⍴)
   v1 ← 3 1⍴ v1
   v1
1
2
3
# Likewise
   v2 ← 3 1⍴ v2
   v3 ← 3 1⍴ v3
   v4 ← 3 1⍴ v4
   v1,v2
1 4
2 5
3 6

# So we need to catenate in a different dimension

   v1⍪v2
1
2
3
4
5
6

#Likewise
   v3⍪v4
 7
 8
 9
10
11
12

# Thats the only parentheses used
   (v1⍪v2),(v3⍪v4)
1  7
2  8
3  9
4 10
5 11
6 12


# And we dont even need all these
   (v1⍪v2),v3⍪v4
1  7
2  8
3  9
4 10
5 11
6 12

[toc] | [prev] | [next] | [standalone]


#93604

FromTony the Tiger <tony@tiger.invalid>
Date2015-07-09 20:23 +0000
Message-ID<7LAnx.3610$JE3.250@fx44.am4>
In reply to#92922
On Sat, 20 Jun 2015 20:57:04 -0500, Nasser M. Abbasi wrote:

> 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

Not exactly sure what it is you want help with, as the answer is in the 
left column of that page you provided the link to.

You can also lose the semicolons. Not needed in Python, other than in 
very special circumstances. More often than not, they're just confusing 
things, IMO.


 /Grrr
-- 
          ___                  ___
 (\_--_/)  | _ ._    _|_|_  _   |o _  _ ._
 ( 9  9 )  |(_)| |\/  |_| |(/_  ||(_|(/_|
 stripes are forever - as overripe ferrets

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web