How to solve this linear algebra equation

I become very bad in linear algebra when matrices are involved in the calculation. Here is one equation:

$$H = (A_2 - a_2p)(A_1 - a_1p)^{-1}$$

where:

$H$, $A_1$ and $A_2$ are $3\times3$ matrices, $a_1$ and $a_2$ are $3\times 1$ matrices $p$ is a $1\times3$ matrix.

$p$ is the only unknown I want to find. How can I do please?

Thanks.

-
@David: No, I think he is fine. $a_i$ is $3\times 1$ and $p$ is $1\times 3$, so $a_i\times p$ is $3\times 3$. –  William Jan 8 '12 at 12:58
Well, what have you tried and where did you get stuck? Perhaps you should try multiplying both sides on the right by $(A_1 - a_1p)$, and then using what you know about matrix arithmetic? –  William Jan 8 '12 at 13:02
My confusion is mainly on the side multiplication. I'm not confident rather I'm suppose to multiply at the left or at the right of one matrices, because it seems the results are not the same depending on the side. But it's ok, I'll take the time to learn linear algebra in detail, for now I just need to implement one algorithm. Thanks a lot –  Gatsu Jan 8 '12 at 13:07
Of course they cannot be the same in general, since matrix multiplication is not commutative (that is, in general, $AB \neq BA$. Therefore, to "cancel" $(A_1 - a_1p)^{-1}$, you must multiply on the correct side - in this case, it is on the right, since on the left you have $(A_2 - a_2p)$ "blocking the way." Hope this explanation helps. –  William Jan 8 '12 at 13:13
Thank you again. At least this will help me to go further next time! –  Gatsu Jan 8 '12 at 13:26

Multiplying on the right by $A_1-a_1p$, one gets $$K=bp\quad\text{with}\quad K=A_2-HA_1\quad\text{and}\quad b=a_2-Ha_1.$$ Recall that $K=(K_{ij})_{1\leqslant i,j\leqslant3}$ is a $3\times3$ matrix, $b=(b_i)_{1\leqslant i\leqslant3}$ is a $3\times1$ (column) vector and one looks for a $1\times3$ (line) vector $p=(p_j)_{1\leqslant j\leqslant 3}$.
That is, $K=bp$ is a shorthand for the condition that $K_{ij}=b_ip_j$ for every $(i,j)$. Thus, $K=bp$ is solved by $$p_j=K_{ij}/b_i$$ for every $(i,j)$ if $K_{ij}/b_i$ does not depend on $i$, and has no solution otherwise. Recall finally that, by definition, $$K_{ij}=(A_2)_{ij}-\sum\limits_{k=1}^3H_{ik}(A_1)_{kj}\quad\text{and}\quad b_i=(a_2)_i-\sum\limits_{k=1}^3H_{ik}(a_1)_k.$$ Edit (This is due to @David Mitra in a comment.) Drawing the matrices $K$ and $bp$ may help to see what is going on: $$\underbrace{ \left[ \matrix {K_{11} & K_{12}& K_{13}\cr K_{21}& K_{22}& K_{23}\cr K_{31}& K_{32}& K_{33}\cr } \right]}_{K=A_2-HA_1}= \underbrace{ \left[ \matrix { b_1p_1& b_1p_2& b_1p_3 \cr b_2p_1& b_2p_2& b_2p_3 \cr b_3p_1& b_3p_2& b_3p_3 \cr } \right]}_{bp=(a_2-Ha_1)p }.$$
This may help to see whats going on (and, thank you Didier for pointing out the gross error in my "solution"): $$\underbrace{ \left[ \matrix {K_{11}& K_{12}&K_{13}\cr K_{21}& K_{22}&K_{23}\cr K_{31}& K_{32}&K_{33}\cr } \right]}_{HA_1-A_2}= \underbrace{ \left[ \matrix { b_1p_1& b_1p_2& b_1p_3 \cr b_2p_1& b_2p_2& b_2p_3 \cr b_3p_1& b_3p_2& b_3p_3 \cr } \right]}_{(Ha_1-a_2)p }$$ –  David Mitra Jan 8 '12 at 13:38
Gatsu: Thanks.  –  Did Jan 8 '12 at 15:08