# Multiplying block matrices

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.

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}}}

• Exactly what I was looking for. Thanks alot. Dec 29, 2014 at 20:01

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 *)


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  *)