0
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ 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] $\endgroup$ – Quantum_Oli May 11 '16 at 9:10
  • 1
    $\begingroup$ See reference.wolfram.com/language/tutorial/…, Ctrl+F "replicate" $\endgroup$ – user484 May 11 '16 at 9:10
  • $\begingroup$ @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. $\endgroup$ – Kay May 11 '16 at 9:25
  • 3
    $\begingroup$ The dropping of rows and columns is shifting the goalposts quite a bit. $\endgroup$ – LLlAMnYP May 11 '16 at 14:07
  • 1
    $\begingroup$ Consider Drop[Array[Reverse[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}] &, {3, 3}, {1, 1}, ArrayFlatten[{##}] &], {3, 6, 3}, {4, 7, 3}]. $\endgroup$ – J. M.'s discontentment May 11 '16 at 14:13
2
$\begingroup$

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]
| improve this answer | |
$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – ciao May 11 '16 at 9:12
  • $\begingroup$ 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 $\endgroup$ – Jason B. May 11 '16 at 9:14
  • $\begingroup$ Yeah, the "100*n,100*n" is at odds with that - like I said, beats me. $\endgroup$ – ciao May 11 '16 at 9:15
  • $\begingroup$ Sometimes I try to answer the question quick and don't wait for them to clarify the question. $\endgroup$ – Jason B. May 11 '16 at 9:17
  • $\begingroup$ @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. $\endgroup$ – Kay May 11 '16 at 9:21
3
$\begingroup$

I like this one:

ArrayFlatten[Table[mat, 100, 100]]
| improve this answer | |
$\endgroup$
2
$\begingroup$
PadRight[#, {10, 10} Dimensions[#], "Periodic"] & @ {{1, 2}, {3, 4}}

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Slightly shorter, you can do PadRight[#, {10, 10} Dimensions[#], #] &@{{1, 2}, {3, 4}} $\endgroup$ – Jason B. May 11 '16 at 9:39
1
$\begingroup$
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

| improve this answer | |
$\endgroup$
0
$\begingroup$

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)$

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.