# How to expand a MxM matrix with replicates of itself and drop some of the rows and cols periodically?

How can I expand a 2d-matrix with dimensions x,x (e.g. mat = {{ 1, 2 }, { 3, 4 }}) with replicates of mat into matTimes100 with dimensions 100*x,100*x? I tried Join[ mat, mat, mat, <97>, 2 ] which works nicely and would give me dimensions 100*x,x. However, I do not want to manually insert mat 100 times into Join[].

How to do that automatically? Join[ Table[ mat, { 1, 100 } ], 2 ] does not work, since it delivers Join[{mat, mat, mat, <97>}, 2 ].

EDIT:

On request, I try to clarify my question. In addition, I try to figure out, what exactly I'm trying to do.

Let's assume a MxM Matrix with M = 3:

mat={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Reverse@mat//TableForm


Now I would like to make a larger NxN matrix out of it with some of the rows and cols dropped in a periodic manner. I tried to visualize my purpose. In the MWE below I choose num = 2 with N = M + num * ( M - 1 ). The dropped rows/cols are indicated by the blue lines. • To clarify, is your goal a 100*x by 100*x matrix or a 100*x by x matrix? Also, you could try Join[Sequence@@Table[mat,100],2] – Quantum_Oli May 11 '16 at 9:10
• See reference.wolfram.com/language/tutorial/…, Ctrl+F "replicate" – user484 May 11 '16 at 9:10
• @Quantum_Oli my goal is a 100*x by 100*x matrix. I just tried to outline my first approach first replicating the rows and than to repeat that with the cols. – Kay May 11 '16 at 9:25
• The dropping of rows and columns is shifting the goalposts quite a bit. – LLlAMnYP May 11 '16 at 14:07
• Consider Drop[Array[Reverse[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}] &, {3, 3}, {1, 1}, ArrayFlatten[{##}] &], {3, 6, 3}, {4, 7, 3}]. – J. M.'s discontentment May 11 '16 at 14:13

Here is one way to do it, using Fold

newmat = Fold[Join[#1, #2, 2] &, mat, ConstantArray[mat, 100]];
MatrixForm@newmat Another method, using ArrayReshape and Transpose

ArrayReshape[Transpose[ConstantArray[mat, 100]], {2, 200}]


Another method using ArrayPad

ArrayPad[mat, {{0, 0}, {0, 200}}, mat]

• These do not produce a 200 x 200 array, which seems to be what the OP is after, but the question is worded with conflicting statements. I think they're after ArrayPad[mat, {{0, 198}, {0, 198}}, "Periodic"], but beats me... – ciao May 11 '16 at 9:12
• Based on the fact that they said Join[ mat, mat, mat, <97>, 2 ] would work perfectly, that wasn't how I interpreted the question, because that would give a 2 by 200 matrix – Jason B. May 11 '16 at 9:14
• Yeah, the "100*n,100*n" is at odds with that - like I said, beats me. – ciao May 11 '16 at 9:15
• Sometimes I try to answer the question quick and don't wait for them to clarify the question. – Jason B. May 11 '16 at 9:17
• @JasonB your answer helped me. I'm looking for a 200 x 200 array, but I'm able to help myself with your answer -> JasonB. – Kay May 11 '16 at 9:21

I like this one:

ArrayFlatten[Table[mat, 100, 100]]

PadRight[#, {10, 10} Dimensions[#], "Periodic"] & @ {{1, 2}, {3, 4}} • Slightly shorter, you can do PadRight[#, {10, 10} Dimensions[#], #] &@{{1, 2}, {3, 4}} – Jason B. May 11 '16 at 9:39
f1 = Drop[ArrayFlatten[ConstantArray[Reverse@#, {#2, #2}]], 3;; ;;3, 4;; ;;3] &

f2 = Drop[KroneckerProduct[ConstantArray[1, {#2 , #2}], Reverse@ #], 3;; ;;3, 4;; ;;3] &

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
n = 5;
f1[mat,n] // MatrixForm f1[mat, n] == f2[mat, n]


True

You can simply use Band

dim = Length[mat]
SparseArray[(Band[{dim # - 1, dim # - 1}] -> mat) & /@ Range];
% // MatrixForm


$\left( \begin{array}{cccccc} 1 & 2 & 0 & 0 & 0 & 0 \\ 3 & 4 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 2 & 0 & 0 \\ 0 & 0 & 3 & 4 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 2 \\ 0 & 0 & 0 & 0 & 3 & 4 \\ \end{array} \right)$

you can do the same thing with Outer also

Outer[Times, IdentityMatrix, mat] // ArrayFlatten;
% // MatrixForm


You can modify the first argument depending on how you want to replicate your matrix. For example if want to get Kuba's answer replace IdentityMatrix by ConstantArray[1, {3, 3}].

If you want to put your matrix row wise, then use Band[{1, dim # - 1}].

dim = Length[mat]
SparseArray[(Band[{1, dim # - 1}] -> mat) & /@ Range];
% // MatrixForm


$\left( \begin{array}{cccccc} 1 & 2 & 1 & 2 & 1 & 2 \\ 3 & 4 & 3 & 4 & 3 & 4 \\ \end{array} \right)$