4
$\begingroup$

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

$\endgroup$
1
  • 3
    $\begingroup$ Please post Mathematica syntax/code, not TeX. $\endgroup$
    – Yves Klett
    Jan 11, 2016 at 19:25

10 Answers 10

9
$\begingroup$

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

Mathematica graphics

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

Join[##, 2] & @@ ConstantArray[mat, n]
$\endgroup$
1
  • $\begingroup$ An alternative to the last expression might be Join[Sequence @@ ConstantArray[mat, n], 2] $\endgroup$
    – Aky
    Jan 12, 2016 at 7:55
8
$\begingroup$

Another way (ArrayFlatten):

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

Mathematica graphics

$\endgroup$
4
$\begingroup$

An alternative approach would be

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

onitsside

... I'll see myself out now.

$\endgroup$
1
4
$\begingroup$

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

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ This answer seems to work but would be more compelling, if applied to the mat in the question. $\endgroup$
    – bbgodfrey
    May 15, 2016 at 19:55
  • $\begingroup$ @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! $\endgroup$
    – anelson
    May 15, 2016 at 20:08
3
$\begingroup$
mat = {{1, 2}, {3, 4}}

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

enter image description here

Or something more general:

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

enter image description here

$\endgroup$
3
$\begingroup$

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}}
$\endgroup$
3
$\begingroup$

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

Mathematica graphics

kpF[mat, 3] // MatrixForm

Mathematica graphics

$\endgroup$
2
$\begingroup$
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}}
$\endgroup$
3
  • $\begingroup$ 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!!! $\endgroup$
    – bmf
    Dec 31, 2023 at 4:10
  • $\begingroup$ Thanks, bmf, I took the desired output from the question. Maybe n + 1 if we imitate the question $\endgroup$
    – eldo
    Dec 31, 2023 at 12:05
  • $\begingroup$ oh yes. of course. I was comparing to some other answers and got confused. Silly mistake on my part. Don't bother :-) $\endgroup$
    – bmf
    Dec 31, 2023 at 12:14
2
$\begingroup$
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*)
$\endgroup$
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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