1
$\begingroup$

This question already has an answer here:

I have a matrix like this (myb1):

myb1 matrix output

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]];

  ]

 ]
$\endgroup$

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ 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). $\endgroup$ – MarcoB Jul 4 '15 at 2:55
  • $\begingroup$ @MarcoB: removed unwanted edits to abate duplication $\endgroup$ – kpnane Jul 4 '15 at 3:16
  • 2
    $\begingroup$ 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. $\endgroup$ – MarcoB Jul 4 '15 at 4:04
  • 1
    $\begingroup$ 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. $\endgroup$ – amr Jul 4 '15 at 5:14
  • $\begingroup$ Thank you again for your response @MarcoB , removed matrix form from code as well $\endgroup$ – kpnane Jul 5 '15 at 5:26
5
$\begingroup$

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}}}}
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – kpnane Jul 4 '15 at 2:47
  • 3
    $\begingroup$ Yes: don't use MatrixForm[]. Then, try again. $\endgroup$ – J. M. will be back soon Jul 4 '15 at 2:49

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