6
$\begingroup$

I have lists of matrices and want to do element-wise matrix multiplication. Is there an easy way to do this that I've missed?

e.g: {A, B, C}.{X, Y, Z} = {A.X, B.Y, C.Z}

where A, B, C, X, Y, and Z are all matrices.

$\endgroup$
1
  • 6
    $\begingroup$ C is not a matrix. C is a reserved symbol for constants, you would be well advised to avoid using C, D, E, I, K, N and O except as their built in functions. $\endgroup$
    – Feyre
    Commented Jul 11, 2016 at 17:49

2 Answers 2

13
$\begingroup$
MapThread[Dot, {{A, B, C}, {X, Y, Z}}]
$\endgroup$
6
$\begingroup$
{a, b, c, x, y, z} = RandomReal[{0, 1}, {6, 2, 2}]

bk = Dot @@@ Transpose@{{a, b, c}, {x, y, z}}
jd = MapThread[Dot, {{a, b, c}, {x, y, z}}]
bk == jd (*True*)

One can also use Inner[Dot, {a, b, c}, {x, y, z}, List] but you need to wrap lists in Unevaluated:

Inner[Dot, Unevaluated@{a, b, c}, Unevaluated@{x, y, z}, List]

See here for explanation.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for pointing out my mistake, it was sloppy of me to not check first. FYI, you could shorten your first line by using RandomReal[{0, 1}, {6,2, 2}]. $\endgroup$
    – Jason B.
    Commented Jul 12, 2016 at 0:55
  • 1
    $\begingroup$ There should be no need for Dot[##] & as Dot can be used directly. $\endgroup$
    – Mr.Wizard
    Commented Jul 12, 2016 at 2:12

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.