# How to repeat a matrix on it's side?

I have the following matrix in Mathematica:

mat = {{0,0,1,1}, {0,1,0,1}}


I want to repeat this matrix N number of times on the side.

For example if N = 3 then the output should be:

$$\begin{matrix} 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1\\ 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1\\ \end{matrix}$$

• Please post Mathematica syntax/code, not TeX. Jan 11, 2016 at 19:25

Join[..., 2] will do it.

Code for your case (and assuming n is not too large):

Join[##, 2] & @@ ConstantArray[mat, n]

• An alternative to the last expression might be Join[Sequence @@ ConstantArray[mat, n], 2]
– Aky
Jan 12, 2016 at 7:55

Another way (ArrayFlatten):

ArrayFlatten[{ConstantArray[mat, n]}, 2]


An alternative approach would be

mat = {{1, 2}, {3, 4}, {5, 6}}
Row[ConstantArray[Rotate[mat // MatrixForm, -Pi/2], 3]]


... I'll see myself out now.

• Cheeky. :P ${}$ Feb 22, 2016 at 18:42

I am a novice user so please forgive me if this is a bit clunky! Using Transpose and ArrayReshape

mat = {{1}, {2}, {3}};
n = 11;
ArrayReshape[Transpose[Table[mat, {n}]], Dimensions[mat] {1, n}] // MatrixForm


• This answer seems to work but would be more compelling, if applied to the mat in the question. May 15, 2016 at 19:55
• @bbgodfrey - thanks for your advice. I was playing around with a few different shapes of mat to see if the solution worked and ended up pasting in one of the other ones! May 15, 2016 at 20:08
mat = {{1, 2}, {3, 4}}

ArrayPad[#, {{0, 0}, {#2, #2}} & @@ Dimensions[#], "Periodic"] & @  mat // MatrixForm


Or something more general:

ArrayPad[#,
{{0, 1}, {1, 2}} {{#, #}, {#2, #2}} & @@ Dimensions[#],
"Periodic"
] & @ mat // MatrixForm


PadRight[]/PadLeft[] can also be used in this case:

mat = {{0, 0, 1, 1}, {0, 1, 0, 1}}; n = 3;
PadRight[ConstantArray[{}, Length[mat]], Dimensions[mat] {1, n}, mat]
{{0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}}


KroneckerProduct

kpF = KroneckerProduct[{ConstantArray[1, #2 ]}, #] &;


SparseArray and Band

saF = SparseArray[(Band[{1, 1}, {1, #2} Dimensions[#], {1,1}] -> #)] &;


Examples:

mat = {{0, 0, 1, 1}, {0, 1, 0, 1}};
saF[mat, 5] // MatrixForm


kpF[mat, 3] // MatrixForm


mat = {{0, 0, 1, 1}, {0, 1, 0, 1}};


Using ReplicateLayer (new in 11.1)

Join @@@ Round @ Transpose @ ReplicateLayer[4] @ mat


returns

{{0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}}

• it's a (+1) from me, but to make it exactly like the desired result, I think it should be ReplicateLayer[3], right? Otherwise, the dimensions don't match. Good stuff nonetheless!!!
– bmf
Dec 31, 2023 at 4:10
• Thanks, bmf, I took the desired output from the question. Maybe n + 1 if we imitate the question
– eldo
Dec 31, 2023 at 12:05
• oh yes. of course. I was comparing to some other answers and got confused. Silly mistake on my part. Don't bother :-)
– bmf
Dec 31, 2023 at 12:14
mat1 = {{0, 0, 1, 1}, {0, 1, 0, 1}};


Using Table and GatherBy:

Flatten /@ GatherBy[Catenate@Table[mat1, {4}]]

(*{{0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}}*)


Using GatherBy to address this problem is possible as long as the matrix doesn't have all its elements equal, but we can generalize this strategy using Partition as shown below:

repeatMat[mat_?MatrixQ, n_Integer?Positive] := Module[{repmat},
repmat = Catenate @ Table[mat, {n}];
If[MatchQ[Flatten @ repmat, {Repeated[m_Integer]}],
Map[Flatten, Partition[repmat, n]],
Map[Flatten, GatherBy @ repmat]
]
];

mat2 = {{1, 1}, {1, 1}};

repeatMat[mat1, 4] === Join[mat1, mat1, mat1, mat1, 2]

(*True*)

repeatMat[mat2, 4] === Join[mat2, mat2, mat2, mat2, 2]

(*True*)


Since @kglr already used KroneckerProduct, I am demonstrating a solution that uses TensorProduct + **ArrayFlatten**

🎊 = ArrayFlatten@TensorProduct[{ConstantArray[1, #2]}, #] &;


and then

🎊[mat, 3]


gives

{{0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}}