2.15 Replicate a matrix

Given Matrix

     1     2 
     3     4
 

Generate a new matrix of size \(2\) by \(3\) where each element of the new matrix is the above matrix. Hence the new matrix will look like

     1     2     1     2     1     2 
     3     4     3     4     3     4 
     1     2     1     2     1     2 
     3     4     3     4     3     4
 

In Matlab, repmat() is used. In Mathematica, a Table command is used, followed by ArrayFlatten[]

Mathematica

mat = {{1,2}, 
       {3,4}} 
 
r = Table[mat,{2},{3}]; 
ArrayFlatten[r]
 

  {{1,2,1,2,1,2}, 
   {3,4,3,4,3,4}, 
   {1,2,1,2,1,2}, 
   {3,4,3,4,3,4}}
 

 

Matlab

A=[1 2;3 4] 
repmat(A,2,3)
 

ans = 
     1     2     1     2     1     2 
     3     4     3     4     3     4 
     1     2     1     2     1     2 
     3     4     3     4     3     4
 

 

Another way is to use kron() in matlab, and KroneckerProduct in Mathematica and LinearAlgebra[KroneckerProduct] in Maple, which I think is a better way. As follows

Mathematica

mat = {{1,2}, 
       {3,4}} 
 
kernel = Table[1, {2}, {3}]; 
   (* {{1, 1, 1}, 
      {1, 1, 1}} *) 
 
KroneckerProduct[kernel, mat]
 

  {{1,2,1,2,1,2}, 
   {3,4,3,4,3,4}, 
   {1,2,1,2,1,2}, 
   {3,4,3,4,3,4}}
 

 

Matlab

A=[1 2;3 4] 
kernel=ones(2,3) 
 
kernel = 
     1     1     1 
     1     1     1 
 
kron(kernel,A)
 

ans = 
     1     2     1     2     1     2 
     3     4     3     4     3     4 
     1     2     1     2     1     2 
     3     4     3     4     3     4
 

 

Maple

A:=Matrix([[1,2],[3,4]]); 
kern:=Matrix([[1,1,1],[1,1,1]]); 
LinearAlgebra:-KroneckerProduct(kern,A);
 

[[1,2,1,2,1,2], 
 [3,4,3,4,3,4], 
 [1,2,1,2,1,2], 
 [3,4,3,4,3,4]]