5
$\begingroup$

I have a 2*8 matrix A, and a 2*8*2 matrix B. So B[[1]] and B[[2]] are both 8*2 matrices. I need a neat way to multiply A by B so that the first list in the result is A.B[[1]] and the second list is A.B[[2]].$$\textbf{A}.\begin{bmatrix}\textbf{B}_1\\\textbf{B}_2\end{bmatrix}=\begin{bmatrix}\textbf{A}.\textbf{B}_1\\\textbf{A}.\textbf{B}_2\end{bmatrix}$$ Where $\textbf{A,B}$ are all compatible matrices. In other words, I want to multiply two partitioned matrices and get the result in the partitioned form also.

Thank you in advance.

$\endgroup$

3 Answers 3

8
$\begingroup$
a = RandomReal[{0, 1}, {2, 8}];
b = RandomReal[{0, 1}, {2, 8, 2}];

a.# & /@ b

{{{2.58906, 3.35618}, {2.5578, 3.12812}}, {{1.3762, 2.87723}, {1.56668, 3.04675}}}

$\endgroup$
1
  • $\begingroup$ Exactly what I was looking for. Thanks alot. $\endgroup$
    – secluded
    Dec 29, 2014 at 20:01
5
$\begingroup$

You can also use

(a.b\[Transpose])\[Transpose]

or, equivalently,

Transpose[a.Transpose[b]]

to get the same result.

a = RandomReal[{0, 1}, {2, 8}];
b = RandomReal[{0, 1}, {2, 8, 2}];
c = a.# & /@ b;  (* from Sjoerd's answer *)

(a.b\[Transpose])\[Transpose] == c
(* True *)
$\endgroup$
3
$\begingroup$

Another way:

a = RandomReal[{0, 1}, {2, 8}];
b = RandomReal[{0, 1}, {2, 8, 2}];

m = First@Outer[Dot, {a}, b, 1];

Compare with Sjoerd's:

s = a.# & /@ b;  (* Sjoerd's *)
s == m
(*  True  *)
$\endgroup$

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.