How does one multiply a list of matrices by a list of vectors, elementwise? For example, multiplying

A = {IdentityMatrix[2], 2*IdentityMatrix[2]}
x = {{1, 1}, {2, -2}}

should return

{{1, 1}, {4, -4}}

Neither Dot nor Times accomplishes this; both have the wrong dimensions. A cumbersome way would be

result = {{0, 0}, {0, 0}}
Do[result[[i]] = A[[i]].x[[i]], {i, 2}]

but surely there is a cleaner way.


3 Answers 3

MapThread[Dot, {A, x}]   // TeXForm

$\left( \begin{array}{cc} 1 & 1 \\ 4 & -4 \\ \end{array} \right)$


Perhaps just:

Dot @@@ Transpose[{A, x}]

Following Belasarius in a somewhat more transparent fashion, you Apply[ ] the Dot function to A and x. The Transpose is used to get the A and x into a single list and the "1" causes the Apply to to work at the correct level.

 Apply[Dot, Transpose[{A, x}], 1]

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.