2.6 Build matrix from other matrices and vectors

2.6.1 First example
2.6.2 second example

2.6.1 First example

Given column vectors \(v1= \left [ {\begin {array}{c} 1\\ 2\\ 3 \end {array}} \right ]\) and \(v2= \left [ {\begin {array}{c} 4\\ 5\\ 6 \end {array}} \right ]\) and \(v3= \left [ {\begin {array}{c} 7\\ 8\\ 9 \end {array}} \right ]\) and \(v4=\left [ {\begin {array}{c} 10\\ 11\\ 12 \end {array}} \right ]\) generate the matrix \(m=\left [ {\begin {array}{cc} v_1&v_2\\ v_3&v_4 \end {array}} \right ] = \left [ {\begin {array}{cc} 1&4\\ \noalign {\medskip }2&5 \\ \noalign {\medskip }3&6\\ \noalign {\medskip }7&10\\ \noalign {\medskip } 8&11\\ \noalign {\medskip }9&12\end {array}} \right ] \)

Matlab was the easiest of all to do these operations with. No surprise, as Matlab was designed for Matrix and vector operations. But I was surprised that Maple actually had good support for these things, using its <> notation, which makes working with matrices and vectors much easier.

The command ArrayFlatten is essential for this in Mathematica.

Notice the need to use Transpose[{v}] in order to convert it to a column matrix. This is needed since in Mathematica, a list can be a row or column, depending on context.

Mathematica

v1 = {1, 2, 3}; v2 = {4, 5, 6}; 
v3 = {7, 8, 9}; v4 = {10, 11, 12}; 
m = ArrayFlatten[ 
 {{Transpose@{v1}, Transpose@{v2}}, 
 {Transpose@{v3}, Transpose@{v4}}} 
     ]
 

\(\left ( {\begin {array}{cc} 1 & 4 \\ 2 & 5 \\ 3 & 6 \\ 7 & 10 \\ 8 & 11 \\ 9 & 12 \\ \end {array}} \right ) \)

 

Matlab

v1=[1,2,3]'; v2=[4,5,6]'; 
v3=[7,8,9]'; v4=[10,11,12]'; 
m=[v1 v2;v3 v4]
 

m = 
     1     4 
     2     5 
     3     6 
     7    10 
     8    11 
     9    12
 

 

Maple

restart; 
v1:=<1,2,3>; #column by default 
v2:=<4,5,6>; 
v3:=<7,8,9>; 
v4:=<10,11,12>; 
m:=< <v1|v2>, 
     <v3|v4>>; 
 
#or using Vector/Matrix notations 
restart; 
v1:=Vector([1,2,3]); #column by default 
v2:=Vector([4,5,6]); #column by default 
v3:=Vector([7,8,9]); #column by default 
v4:=Vector([10,11,12]); #column by default 
m:=Matrix([ [v1,v2],[v3,v4]])
 

\(\left [ {\begin {array}{cc} 1&4\\ \noalign {\medskip }2&5 \\ \noalign {\medskip }3&6\\ \noalign {\medskip }7&10\\ \noalign {\medskip } 8&11\\ \noalign {\medskip }9&12\end {array}} \right ] \)

 

Python

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)); 
 
r1 =np.hstack([(v1,v2)]).T 
r2 =np.hstack([(v3,v4)]).T 
mat = np.vstack((r1,r2))
 

Another way

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[211]: 
array([[ 1,  4], 
       [ 2,  5], 
       [ 3,  6], 
       [ 7, 10], 
       [ 8, 11], 
       [ 9, 12]])
 

 

Fortran

PROGRAM main   IMPLICIT none 
   INTEGER :: i 
   INTEGER , DIMENSION(3) :: v1,v2,v3,v4 
   INTEGER , DIMENSION(6,2) :: m 
   v1 = [1,2,3]; v2=[4,5,6]; 
   v3=[7,8,9];   v4=[10,11,12]; 
 
   m(1:3,1) = v1; m(1:3,2)=v2; 
   m(4:,1)=v3;  m(4:,2)=v4; 
 
   DO i=1,size(m,1) 
      PRINT *, m(i,:) 
   END DO 
 
END PROGRAM main
 

Using the RESHAPE command

PROGRAM main   IMPLICIT none 
   INTEGER :: i 
   INTEGER , DIMENSION(3) :: v1,v2,v3,v4 
   INTEGER , DIMENSION(6,2) :: m 
   v1 = [1,2,3]; v2=[4,5,6]; 
   v3=[7,8,9]; v4=[10,11,12]; 
 
   m = RESHAPE([v1,v3,v2,v4], SHAPE(m), ORDER=[1,2]) 
 
   DO i=1,size(m,1) 
      PRINT *, m(i,:) 
   END DO 
 
END PROGRAM main
 

>gfortran -Wall foo.f90 
>./a.out 
1           4 
2           5 
3           6 
7          10 
8          11 
9          12
 

 

2.6.2 second example

Given mix of matrices and vectors, such as \(v1= \left [ {\begin {array}{c} 1\\ 2\\ 3 \end {array}} \right ]\) and \(v2= \left [ {\begin {array}{cc} 4& 5\\ 6& 7\\ 8& 9 \end {array}} \right ]\) and \(v3= \left [ {\begin {array}{c} 10\\ 11\\ 12 \end {array}} \right ]\) and \(v4=\left [ {\begin {array}{c} 13\\ 14\\ 15 \end {array}} \right ]\) and

\(v5=\left [ {\begin {array}{c} 16\\ 17\\ 18 \end {array}} \right ]\)

generate the matrix 6 by 3 matrix \(m= \left [ {\begin {array}{ccc} v1&v2\\v3&v4&v5\end {array}}\right ]= \left [ {\begin {array}{ccc} 1&4&5\\ \noalign {\medskip }2&6&7 \\ \noalign {\medskip }3&8&9\\ \noalign {\medskip }10&13&16 \\ \noalign {\medskip }11&14&17\\ \noalign {\medskip }12&15&18\end {array}} \right ] \)

Mathematica, thanks for function by Kuba at Mathematica stackexachage, this becomes easy to do

Mathematica

myArrayFlatten = Flatten /@ Flatten[#, {{1, 3}}] & 
 
v1 = {1, 2, 3}; 
v2 = {{4, 5}, {6, 7}, {8, 9}}; 
v3 = {10, 11, 12}; 
v4 = {13, 14, 15}; 
v5 = {16, 17, 18}; 
 
m={ 
  {v1, v2}, 
  {v3, v4, v5}, 
} // myArrayFlatten
 

Maple

restart; 
v1:=<1,2,3>; #column by default 
v2:=<<4|5>, 
     <6|7>, 
     <8|9>>; 
v3:=<10,11,12>; 
v4:=<13,14,15>; 
v5:=<16,17,18>; 
m:=< <v1|v2>, 
     <v3|v4|v5>>;
                                                                                    
                                                                                    
 

Matlab

v1=[1,2,3]'; 
v2=[4,5;6,7;8,9]; 
v3=[10,11,12]'; 
v4=[13,14,15]'; 
v5=[16,17,18]'; 
 
m=[v1 v2;v3 v4 v5]
 

Fortran

PROGRAM main   IMPLICIT none 
   INTEGER :: i 
   INTEGER , DIMENSION(3)   :: v1,v3,v4,v5 
   INTEGER , DIMENSION(3,2) :: v2 = RESHAPE([4,6,8,5,7,9],SHAPE(v2),ORDER=[1,2]) 
   INTEGER , DIMENSION(6,3) :: m 
   v1 = [1,2,3];   v3=[10,11,12]; 
   v4 =[13,14,15]; v5=[16,17,18]; 
   m = RESHAPE([v1,v3,v2(:,1),v4,v2(:,2),v5], SHAPE(m), ORDER=[1,2]) 
 
   DO i=1,size(m,1) 
      PRINT *, m(i,:) 
   END DO 
 
END PROGRAM main
 
>gfortran -Wall foo.f90 
>./a.out 
           1           4           5 
           2           6           7 
           3           8           9 
          10          13          16 
          11          14          17 
          12          15          18