Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question
    
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
2  
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
add comment

2 Answers 2

'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$$

share|improve this answer
    
Agks doesn't want the first column but the diagonal, no? –  user23211 May 18 '12 at 20:15
    
Thanks for spotting that. –  copper.hat May 18 '12 at 20:22
    
thank you -- can the summation be expressed in pure matrix terms? –  agks mehx May 18 '12 at 20:25
    
If you mean by just products, transpositions and multiplication by constant matrices then I doubt it. –  copper.hat May 18 '12 at 21:06
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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