0

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.

Thanks for your hints!!

enter image description here

Kay
  • 1,035
  • 7
  • 19

5 Answers5

3

I like this one:

ArrayFlatten[Table[mat, 100, 100]]
LLlAMnYP
  • 11,486
  • 26
  • 65
2

Here is one way to do it, using Fold

newmat = Fold[Join[#1, #2, 2] &, mat, ConstantArray[mat, 100]];
MatrixForm@newmat

enter image description here

Another method, using ArrayReshape and Transpose

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

Another method using ArrayPad

ArrayPad[mat, {{0, 0}, {0, 200}}, mat]
Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • 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 09: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 09:14
  • Yeah, the "100n,100n" is at odds with that - like I said, beats me. – ciao May 11 '16 at 09: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 09: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 09:21
  • @ciao you're right. Maybe the question is not asked properly. I think this usually comes with the fact of not being a native speaker... – Kay May 11 '16 at 09:22
2
PadRight[#, {10, 10} Dimensions[#], "Periodic"] & @ {{1, 2}, {3, 4}}

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
1
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

Mathematica graphics

f1[mat, n] == f2[mat, n]

True

kglr
  • 394,356
  • 18
  • 477
  • 896
0

You can simply use Band

dim = Length[mat]
SparseArray[(Band[{dim # - 1, dim # - 1}] -> mat) & /@ Range[3]];
% // 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[3], 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[3] 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[3]];
% // MatrixForm

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

Sumit
  • 15,912
  • 2
  • 31
  • 73