# Matrix Multiplication

I have a question about matrix multiplication. I have 3 matrix which are Ce matrix and its dimension is 3x3 . The second matrix is Be and it is 3x18x2 and the third matrix is del matrix and its dimension is 18x1.

I want to multiply three matrix as Ce . Be[[, , 1]] . del and Ce . Be [[, , 2] . del

If Be matrix is just 3x18 there is no problem but here its third dimension is 2 and I dont know how to multiply each of them. Could you help me ?

Best Regards;

Ahmet;

The second matrix be has two pages. Each page is 3 by 18, so when you multiply, just select the page you want to use. For example, to use the first page of be, then

ce = RandomInteger[{1, 10}, {3, 3}];
be = RandomInteger[{1, 10}, {3, 18, 2}]
del = RandomInteger[{1, 10}, {18, 1}]


and now

ce.be[[All, All, 2]].del


To use the first page, do

ce.be[[All, All, 1]].del


And to do all pages at once,

(ce.be[[All, All, #]].del) & /@ Range@Dimensions[be][[3]]


MatrixForm[be[[All, All, 1]]]


MatrixForm[be[[All, All, 2]]]


• Transpose[ce.Transpose[be, {1, 3, 2}]].del also works. Dec 26, 2016 at 11:08

Another possibility is to use TensorContract/TensorProduct:

ce=RandomInteger[{1,10},{3,3}];
be=RandomInteger[{1,10},{3,18,2}];
del=RandomInteger[{1,10},{18,1}];

(ce.be[[All,All,#]].del)&/@Range[2]
(* output: {{{5591},{12404},{10529}},{{4463},{10040},{8921}}} *)

Transpose[ce.Transpose[be,{1,3,2}]].del
(* {{{5591},{12404},{10529}},{{4463},{10040},{8921}}} *)

TensorContract[TensorProduct[be,ce,del],{{2,6},{1,5}}]
(* {{{5591},{12404},{10529}},{{4463},{10040},{8921}}} *)


Basically, TensorProduct[be, ce, del] has dimensions {3,18,2,3,3,18,1}, and the given TensorContract call eliminates (contracts) indices 1, 2, 5 and 6, leaving an array of dimensions {2, 3, 1}.