# Interleaving the columns of two matrices horizontally in a particular way

I have two matrices:

Matrix1 =
Table[NumberForm[N[(10 i + 9 j)/70, 3], {4, 2}], {i, 33}, {j, 18}];

MatrixForm[Matrix1]

Matrix2 =
Table[NumberForm[N[(12 i + 17 j)/70, 3], {4, 2}], {i, 33}, {j, 13}];

MatrixForm[Matrix2]


I want to "interleave" the first column of Matrix 1 horizontally with all the columns of Matrix 2. Then the second column of Matrix 1 with all the columns of Matrix 2; and continue like this until the last column of Matrix 1 has been interleaved with all the columns of Matrix 2. The following is my attempt to do this; here I have only gone as far as interleaving the first six columns of Matrix 1 with all the columns of Matrix 2; (I stopped at 6 for decorum sake...)

Table[{Matrix1[[i]][[1]], Matrix2[[i]][[1]], Matrix1[[i]][[1]],
Matrix2[[i]][[2]], Matrix1[[i]][[1]], Matrix2[[i]][[3]],
Matrix1[[i]][[1]], Matrix2[[i]][[4]], Matrix1[[i]][[1]],
Matrix2[[i]][[5]], Matrix1[[i]][[1]], Matrix2[[i]][[6]],
Matrix1[[i]][[1]], Matrix2[[i]][[7]], Matrix1[[i]][[1]],
Matrix2[[i]][[8]], Matrix1[[i]][[1]], Matrix2[[i]][[9]],
Matrix1[[i]][[1]], Matrix2[[i]][[10]], Matrix1[[i]][[1]],
Matrix2[[i]][[11]], Matrix1[[i]][[1]], Matrix2[[i]][[12]],
Matrix1[[i]][[1]], Matrix2[[i]][[13]], Matrix1[[i]][[2]],
Matrix2[[i]][[1]], Matrix1[[i]][[2]], Matrix2[[i]][[2]],
Matrix1[[i]][[2]], Matrix2[[i]][[3]], Matrix1[[i]][[2]],
Matrix2[[i]][[4]], Matrix1[[i]][[2]], Matrix2[[i]][[5]],
Matrix1[[i]][[2]], Matrix2[[i]][[6]], Matrix1[[i]][[2]],
Matrix2[[i]][[7]], Matrix1[[i]][[2]], Matrix2[[i]][[8]],
Matrix1[[i]][[2]], Matrix2[[i]][[9]], Matrix1[[i]][[2]],
Matrix2[[i]][[10]], Matrix1[[i]][[2]], Matrix2[[i]][[11]],
Matrix1[[i]][[2]], Matrix2[[i]][[12]], Matrix1[[i]][[2]],
Matrix2[[i]][[13]], Matrix1[[i]][[3]], Matrix2[[i]][[1]],
Matrix1[[i]][[3]], Matrix2[[i]][[2]], Matrix1[[i]][[3]],
Matrix2[[i]][[3]], Matrix1[[i]][[3]], Matrix2[[i]][[4]],
Matrix1[[i]][[3]], Matrix2[[i]][[5]], Matrix1[[i]][[3]],
Matrix2[[i]][[6]], Matrix1[[i]][[3]], Matrix2[[i]][[7]],
Matrix1[[i]][[3]], Matrix2[[i]][[8]], Matrix1[[i]][[3]],
Matrix2[[i]][[9]], Matrix1[[i]][[3]], Matrix2[[i]][[10]],
Matrix1[[i]][[3]], Matrix2[[i]][[11]], Matrix1[[i]][[3]],
Matrix2[[i]][[12]], Matrix1[[i]][[3]], Matrix2[[i]][[13]],
Matrix1[[i]][[4]], Matrix2[[i]][[1]], Matrix1[[i]][[4]],
Matrix2[[i]][[2]], Matrix1[[i]][[4]], Matrix2[[i]][[3]],
Matrix1[[i]][[4]], Matrix2[[i]][[4]], Matrix1[[i]][[4]],
Matrix2[[i]][[5]], Matrix1[[i]][[4]], Matrix2[[i]][[6]],
Matrix1[[i]][[4]], Matrix2[[i]][[7]], Matrix1[[i]][[4]],
Matrix2[[i]][[8]], Matrix1[[i]][[4]], Matrix2[[i]][[9]],
Matrix1[[i]][[4]], Matrix2[[i]][[10]], Matrix1[[i]][[4]],
Matrix2[[i]][[11]], Matrix1[[i]][[4]], Matrix2[[i]][[12]],
Matrix1[[i]][[4]], Matrix2[[i]][[13]], Matrix1[[i]][[5]],
Matrix2[[i]][[1]], Matrix1[[i]][[5]], Matrix2[[i]][[2]],
Matrix1[[i]][[5]], Matrix2[[i]][[3]], Matrix1[[i]][[5]],
Matrix2[[i]][[4]], Matrix1[[i]][[5]], Matrix2[[i]][[5]],
Matrix1[[i]][[5]], Matrix2[[i]][[6]], Matrix1[[i]][[5]],
Matrix2[[i]][[7]], Matrix1[[i]][[5]], Matrix2[[i]][[8]],
Matrix1[[i]][[5]], Matrix2[[i]][[9]], Matrix1[[i]][[5]],
Matrix2[[i]][[10]], Matrix1[[i]][[5]], Matrix2[[i]][[11]],
Matrix1[[i]][[5]], Matrix2[[i]][[12]], Matrix1[[i]][[5]],
Matrix2[[i]][[13]], Matrix1[[i]][[6]], Matrix2[[i]][[1]],
Matrix1[[i]][[6]], Matrix2[[i]][[2]], Matrix1[[i]][[6]],
Matrix2[[i]][[3]], Matrix1[[i]][[6]], Matrix2[[i]][[4]],
Matrix1[[i]][[6]], Matrix2[[i]][[5]], Matrix1[[i]][[6]],
Matrix2[[i]][[6]], Matrix1[[i]][[6]], Matrix2[[i]][[7]],
Matrix1[[i]][[6]], Matrix2[[i]][[8]], Matrix1[[i]][[6]],
Matrix2[[i]][[9]], Matrix1[[i]][[6]], Matrix2[[i]][[10]],
Matrix1[[i]][[6]], Matrix2[[i]][[11]], Matrix1[[i]][[6]],
Matrix2[[i]][[12]], Matrix1[[i]][[6]], Matrix2[[i]][[13]]}, {i, 1,
33}] // MatrixForm


• Transpose@Riffle[Transpose@Matrix1, Transpose@Matrix2] ? Aug 26, 2016 at 18:40
• To George2079; your command riffles the two matrices but, it is not interleaving them in the manner that I described above. Notice that the first column of Matrix 1 is interleaved with each column of Matrix 2. Next the second column of Matrix 1 is interleaved with each column of Matrix 2; etc. ; until the last column of Matrix 1 is interleaved with each column of Matrix 2. Thank you for your attempt though! Aug 26, 2016 at 18:57
• can you make a smaller example so you can show the whole result? What are the dimensions of the result? Aug 26, 2016 at 19:14

Possible approaches with Riffle:

dim1 = Last@Dimensions[Matrix1];
dim2 = Last@Dimensions[Matrix2];
tMatrix2 = Transpose[Matrix2];

Transpose[Join @@
(Riffle[ConstantArray[Matrix1[[All, #]], dim2], tMatrix2] & /@ Range[dim1])
]


or

Transpose[
Riffle[
Join @@ (ConstantArray[#, Last@Dimensions[Matrix2]] & /@ Transpose[Matrix1]),
Transpose[Matrix2],
{2, -1, 2}]
]


An approach with Outer:

Transpose[Join @@ Outer[Sequence, Transpose[Matrix1], Transpose[Matrix2], 1]]

• Thank you Xavier (and thank you George)! Indeed that last code does exactly what I need! Thanks! Aug 26, 2016 at 19:43