4
$\begingroup$

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;

$\endgroup$

2 Answers 2

4
$\begingroup$

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

Mathematica graphics

To use the first page, do

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

Mathematica graphics

And to do all pages at once,

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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