# How to express this using matrix operations?

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?

• 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) – Ilya May 18 '12 at 20:16
• Also $\operatorname{diag}(AB^T)$ gives the vector. (Horizontal or vertical, depending on how we define the operator $\operatorname{diag}$.) – user23211 May 18 '12 at 20:18
• 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. – agks mehx May 18 '12 at 20:19
• thank you for your responses; i am trying to understand/verify them. – agks mehx May 18 '12 at 20:20

'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$$
$$c_i = \sum_{j=1}^3 a_{ij}b_{ij}$$