4

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

Kuba
  • 136,707
  • 13
  • 279
  • 740
Yahya Uddin
  • 165
  • 6

10 Answers10

9

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

Mathematica graphics

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

Join[##, 2] & @@ ConstantArray[mat, n]
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • An alternative to the last expression might be Join[Sequence @@ ConstantArray[mat, n], 2] – Aky Jan 12 '16 at 07:55
8

Another way (ArrayFlatten):

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

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747
4

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.

LLlAMnYP
  • 11,486
  • 26
  • 65
4

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

anelson
  • 41
  • 1
  • 4
  • 2
    This answer seems to work but would be more compelling, if applied to the mat in the question. – bbgodfrey May 15 '16 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 '16 at 20:08
3
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

Kuba
  • 136,707
  • 13
  • 279
  • 740
3

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}}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
3

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

kglr
  • 394,356
  • 18
  • 477
  • 896
2
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}}
eldo
  • 67,911
  • 5
  • 60
  • 168
  • 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!!! – bmf Dec 31 '23 at 04:10
  • Thanks, bmf, I took the desired output from the question. Maybe n + 1 if we imitate the question – eldo Dec 31 '23 at 12:05
  • oh yes. of course. I was comparing to some other answers and got confused. Silly mistake on my part. Don't bother :-) – bmf Dec 31 '23 at 12:14
2
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)

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
2

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

bmf
  • 15,157
  • 2
  • 26
  • 63