# Matrix multiplication for higher dimensional matrices [duplicate]

I have a matrix like this (myb1): I would like to get the matrix multiplication of each of the submatrix to itself. For example the first $2\times2$ matrix $\begin{bmatrix}1 & 2\\1 & 2\end{bmatrix}$ should be multiplied by itself and answer of res2[[1, 1]] be $\begin{bmatrix}3 & 6\\3 & 6\end{bmatrix}$ and so on.

I tried: for loop (in the picture) and didn't help

Direct Mathematica code: myb1.myb1 and it didn't work out either

### Mathematica Code

myb1[[1, 1]] = {{1, 2}, {1, 2}}

myb1[[1, 2]] = {{2, 3}, {3, 4}}

myb1[[2, 1]] = {{5, 2}, {8, 2}}

myb1[[2, 2]] = {{1, 2}, {1, 2}}

For[i = 1, i <= 2, i++,

For[j = 1, j <= 2, j++,

res2 = myb1[[i, j]].myb1[[i, j]];

]

]


## marked as duplicate by MarcoB, m_goldberg, Bob Hanlon, Sjoerd C. de Vries, JensJul 4 '15 at 22:26

• Here is a link to a FAQ post that explains why it's typically a bad idea to use MatrixForm, no matter how alluring its nicely formatted output might be: (18393). – MarcoB Jul 4 '15 at 2:55
• @MarcoB: removed unwanted edits to abate duplication – kpnane Jul 4 '15 at 3:16
• You will still want to avoid using MatrixForm as you are showing. If you absolutely need to have pretty-printed matrices, then at least use: (myb1 = {{1, 2}, {1, 2}}) // MatrixForm;. Note the () brackets: they change the order of execution so that the variable myb1 is assigned the "raw" matrix, before the MatrixForm is evaluated. – MarcoB Jul 4 '15 at 4:04
• to clarify here, MatrixForm is a display function. it's used to show your results as a matrix, but is not used during calculations. you don't need to tell Mathematica "hey this is a matrix" because any rectangular list of lists is as good a matrix as you need. for example Det[{{a, 1}, {Graphics[Disk[]], d}}] will give an answer. this is a deliberate design feature. in particular, it means that all the functions you use for dealing with lists (such as Reverse) can also be used directly on matrices... and vice versa. – amr Jul 4 '15 at 5:14
• Thank you again for your response @MarcoB , removed matrix form from code as well – kpnane Jul 5 '15 at 5:26

For the love of this fine site, please post copyable code along with accompanying images the next time!

Anyway:

myb1 = {{{{1, 2}, {1, 2}}, {{2, 3}, {3, 4}}},
{{{5, 2}, {8, 2}}, {{1, 2}, {1, 2}}}};

Map[MatrixPower[#, 2] &, myb1, {2}]
{{{{3, 6}, {3, 6}}, {{13, 18}, {18, 25}}},
{{{41, 14}, {56, 20}}, {{3, 6}, {3, 6}}}}

Map[#.# &, myb1, {2}]
{{{{3, 6}, {3, 6}}, {{13, 18}, {18, 25}}},
{{{41, 14}, {56, 20}}, {{3, 6}, {3, 6}}}}

• I tried your suggestion -- can you look at my edit and tell me how to get the final answer instead of dots between matrices inside the big matrix – kpnane Jul 4 '15 at 2:47
• Yes: don't use MatrixForm[]. Then, try again. – J. M. will be back soon Jul 4 '15 at 2:49