Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
3  
Please post Mathematica syntax/code, not TeX. – Yves Klett Jan 11 at 19:25
up vote 7 down vote accepted

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

Mathematica graphics

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

Join[##, 2] & @@ ConstantArray[mat, n]
share|improve this answer
    
An alternative to the last expression might be Join[Sequence @@ ConstantArray[mat, n], 2] – Aky Jan 12 at 7:55

Another way (ArrayFlatten):

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

Mathematica graphics

share|improve this answer
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

share|improve this answer

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}}
share|improve this answer

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.

share|improve this answer
    
Cheeky. :P ${}$ – J. M. Feb 22 at 18:42

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

share|improve this answer
2  
This answer seems to work but would be more compelling, if applied to the mat in the question. – bbgodfrey May 15 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 at 20:08

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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