6

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.

Zar
  • 73
  • 1
  • 4
  • 6
    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. – Feyre Jul 11 '16 at 17:49

2 Answers2

13
MapThread[Dot, {{A, B, C}, {X, Y, Z}}]
John Doty
  • 13,712
  • 1
  • 22
  • 42
6
{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.

BlacKow
  • 6,428
  • 18
  • 32
  • 1
    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}]. – Jason B. Jul 12 '16 at 00:55
  • 1
    There should be no need for Dot[##] & as Dot can be used directly. – Mr.Wizard Jul 12 '16 at 02:12