0
$\begingroup$

I have a matrix $A$:

$$A=\begin{pmatrix} 1 &3 &1\\ 7 &5 &2\\ 4& 3& 7\\ 8& 2& 1\\ 3& 9& 6\\ 4 &5 &2 \end{pmatrix}$$

and a matrix $B$:

$$\begin{pmatrix} 2& 9& 1\\ 4& 3 &8\\ 9& 7& 3\\ 4& 4& 2\\ 6& 5& 7\\ 2 &9& 2 \end{pmatrix}$$

I want to compute $C$:

$$\begin{pmatrix} 1\cdot2+3\cdot9+1\cdot1\\ 7\cdot4+5\cdot3+2\cdot8\\ 4\cdot9+3\cdot7+7\cdot3\\ 8\cdot4+2\cdot4+1\cdot2\\ 3\cdot6+9\cdot5+6\cdot7\\ 4\cdot2+5\cdot9+2\cdot2 \end{pmatrix}$$

How can I express this purely using matrix operations?

$\endgroup$
  • $\begingroup$ Would you tell why are you interested in this? This can be done via $$ C = (A.B)\begin{pmatrix}1\\1\\1\end{pmatrix} $$ where $A.B$ denotes the element-wise product which is not quite a pure matrix operation (although it is implemented e.g. in MATLAB) $\endgroup$ – Ilya May 18 '12 at 20:16
  • 2
    $\begingroup$ Also $\operatorname{diag}(AB^T)$ gives the vector. (Horizontal or vertical, depending on how we define the operator $\operatorname{diag}$.) $\endgroup$ – user23211 May 18 '12 at 20:18
  • $\begingroup$ I am interested in this because I am trying to compute the cost function of a neural network. I have a 5000 x 10 output vector and a 5000 x 10 training vector. I. e. it is classifying a training set of 5000 things into 10 classes. $\endgroup$ – agks mehx May 18 '12 at 20:19
  • $\begingroup$ thank you for your responses; i am trying to understand/verify them. $\endgroup$ – agks mehx May 18 '12 at 20:20
1
$\begingroup$

'Switch' the $B$ matrix around with a transpose: $A B^T$. Let $e_1 = (1,0,0,0,0,0)^T$, $e_2 = (0,1,0,0,0,0)^T$, etc. Then: $$C = \sum_{i=1}^6 (e_i^T A B^T e_i)e_i$$

$\endgroup$
  • $\begingroup$ Agks doesn't want the first column but the diagonal, no? $\endgroup$ – user23211 May 18 '12 at 20:15
  • $\begingroup$ thank you -- can the summation be expressed in pure matrix terms? $\endgroup$ – agks mehx May 18 '12 at 20:25
  • $\begingroup$ If you mean by just products, transpositions and multiplication by constant matrices then I doubt it. $\endgroup$ – copper.hat May 18 '12 at 21:06
0
$\begingroup$

From a non-MATLAB perspective, you're just taking the dot product of the rows of each matrix so

$$ c_i = \sum_{j=1}^3 a_{ij}b_{ij} $$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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