Hello I would like to multiply a list of matrices by another list of matrices and return all elements. I.e.


And the result should be


I tried


but it did not work.

  • 3
    $\begingroup$ Does Dot @@@ Tuples[{{a, b, c}, {x, y}}] give what you need? $\endgroup$ – kglr Jan 19 '17 at 1:00
  • 1
    $\begingroup$ .. or Join @@ Transpose@Outer[Dot, {a, b, c}, {x, y}]? $\endgroup$ – kglr Jan 19 '17 at 1:05
  • $\begingroup$ It does! The order is slightly different - it is {a.x, a.y,....} but it doesn't matter. Thanks! $\endgroup$ – MathsAndBlocks Jan 19 '17 at 1:11
  • $\begingroup$ @kglr, make an answer dude and we will upvote it!! $\endgroup$ – ivbc Jan 19 '17 at 1:29
Dot @@@ Tuples[{{a, b, c}, {x, y}}]
(* or *) Join @@ Transpose@Outer[Dot, {a, b, c}, {x, y}]
(* or *) Distribute[{{a, b, c}, {x, y}}, List, List, List, Dot]

{a.x, a.y, b.x, b.y, c.x, c.y}

If the order matters use SortBy:

Dot @@@ Tuples[{{a, b, c}, {x, y}}] // SortBy[#, Last]&

{a.x, b.x, c.x, a.y, b.y, c.y}

Or, if you have version 10 or 11, just SortBy[Last] instead of SortBy[#, Last]&.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.