3
$\begingroup$

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

Thank you for your help!

$\endgroup$
3
  • $\begingroup$ Transpose@Riffle[Transpose@Matrix1, Transpose@Matrix2] ? $\endgroup$
    – george2079
    Aug 26, 2016 at 18:40
  • $\begingroup$ 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! $\endgroup$ Aug 26, 2016 at 18:57
  • $\begingroup$ can you make a smaller example so you can show the whole result? What are the dimensions of the result? $\endgroup$
    – george2079
    Aug 26, 2016 at 19:14

1 Answer 1

4
$\begingroup$

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]]
$\endgroup$
1
  • $\begingroup$ Thank you Xavier (and thank you George)! Indeed that last code does exactly what I need! Thanks! $\endgroup$ Aug 26, 2016 at 19:43

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.