# 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. – Yves Klett Jan 11 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 at 7:55

Another way (ArrayFlatten):

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


-
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}}

-

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 ${}$ – J. M. Feb 22 at 18:42

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


-

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. – bbgodfrey May 15 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! – anelson May 15 at 20:08