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
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
Python
Fortran
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]
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