# How to list the pairwise matrix products of two lists of matrices

I have two lists of 3x3 matrices, of equal length. I would like to make a list of their pairwise matrix products. Is there a more elegant way to do it than this ?

rotationmatrices={{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}};
scalematrices={{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 2, 0}, {0, 0, 1}}, {{2, 0, 0}, {0, 1, 0}, {0, 0, 2}}};
Map[Apply[Dot, Transpose[{rotationmatrices, scalematrices}][[#]]] &, Range[Length[rotationmatrices]]]


I also tried

Inner[Dot, rotationmatrices, scalematrices, List]


and

Inner[Dot[#1, #2] &, rotationmatrices, scalematrices, List]


but neither of these worked.

• MapThread[Dot, {rotationmatrices, scalematrices}]? Aug 14, 2017 at 11:33
• Brilliant ! Thank you J. M. !! Aug 14, 2017 at 11:39

If you want all products between rotation and scaling matrices you want Outer.

(You know you have some repeated elements in your scale matrices, right?)

Here's one way of seeing all the Dots between each rotation and each scale matrix, and understanding where they came from:

 allDots= ((Outer[dot[rot[Position[rotationmatrices, #1] // Flatten],
scale[Position[scalematrices, #2] // Flatten]] ->
Dot[#1, #2] &, rotationmatrices, scalematrices, 1] //
Flatten) // Union );


If you just want the dot of matrices between matrices with the same index in your lists, you should use J.M.'s suggestion of MapThread;

orderedDots1= MapThread[Dot, {rotationmatrices, scalematrices}];


This is equivalent to (but slightly faster than)

orderedDots2= MapIndexed[Dot[#, scalematrices[[#2[[1]]]]] &, rotationmatrices];

Dot @@@ Transpose[{rotationmatrices, scalematrices}]


{{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 0, -1}, {0, 2, 0}}, {{0, 1, 0}, {-2, 0, 0}, {0, 0, 2}}}