# Quick help needed on a Matrix Multiplication

1. Jul 1, 2011

### Yondaime5685

I have a small problem where I remember I learned it in the past. But now I can't seem to recall how to do it. I searched on google, but I think even though it is probably really easy, I may not have worded it right to get the references I want. So I decided to ask here.

Here is the problem:

Lets say I have this 4x3 matrix: [1,2,3;5,6,7;9,0,1;3,4,5]

What matrix can I multiply this with or by to get back, for this example, 1&2 in the first row and 6&7 in the second row and the rest should be zero...?

Now what is that general structure I need to make the second matrix to get back or be able to "pick off" any value from the first matrix that I choose..?

I know this is an easy question but I don't have my Math Physics book with me and I'm terrible at wording what I need to ask. Haha.

Thanks in advance.

2. Jul 1, 2011

### micromass

Staff Emeritus
Hi Yondaime5685!

Let Ei be the 4x4 matrix with a 1 on place (i,i) and a zero elsewhere. Let Ei' be the 3x3 matrix with a 1 on place (i,i) and a zero elsewhere. Let A be your matrix.

To obtain a matrix which "picks off" the value (i,j) of A, you just do

$$E_iAE_j^\prime$$

For example

$$\left(\begin{array}{cccc} 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ \end{array}\right) \left(\begin{array}{ccc} 1 & 2 & 3\\ 5 & 6 & 7\\ 9 & 0 & 1\\ 3 & 4 & 5\\ \end{array}\right) \left(\begin{array}{cccc} 0 & 0 & 0\\ 0 & 0 & 0\\ 0 & 0 & 1\\ \end{array}\right) = \left(\begin{array}{cccc} 0 & 0 & 0\\ 0 & 0 & 7\\ 0 & 0 & 0\\ 0 & 0 & 0\\ \end{array}\right)$$

Can you such a reasoning to solve your problem?

Last edited: Jul 1, 2011
3. Jul 1, 2011

### chogg

I don't think there is a solution to do what you want. Let me explain why I think so.

First, I'll try to state your goal. You are trying to take an $m \times n$ matrix ("$A$"), multiply it with other matrices, and get back another $m \times n$ matrix ("$B$"). The most general way to do that is to hit it from both sides. So you're looking for an equation of the form
$$B = MAN$$
where $M$ is an $m \times m$ matrix, and $N$ is an $n \times n$ matrix. Your goal: the entries of $B$ should be either the same as the corresponding entries in $A$, or zero... and this should hold for any values of $A$.

Does that sound right?

Now, consider an element that you want to set to zero. Say, $B_{ij}=0 \forall A$. Well, we have
$$B_{ij} = \sum_k \sum_l M_{ik} A_{kl} N_{lj} = \sum_k\sum_l (M_{ik}N_{lj}) A_{kl}$$
In other words: some particular linear combination of the entries of $A$ adds up to zero. Does it hold for other values of $A$? Plainly not: if we change element $A_{kl}$ by $\delta$, then that "zero" element $B_{ij} = \delta M_{ik}N_{lj} \ne 0$.

So, it seems to me you can't mask out components of matrices by matrix multiplication.

4. Jul 1, 2011

### chogg

Silly me! Of course you can make it work by setting, for instance, $M_{ik} = 0$ for all k. This will mask out the entire i'th row of B.

Alternatively, setting $N_{lj}=0$ for all l masks out the j'th column of B.

So it seems you can mask off parts of a matrix, but you have to do a whole row or whole column at a time.

If you want to pick out an individual matrix element -- say, element (i,j) -- setup M so that its i'th diagonal element is 1 (and all the rest are zero), and N so that its j'th diagonal element is 1 (and all the rest are zero).

I should refrain from posting except after a good night's sleep. :P :)

5. Jul 1, 2011

### Yondaime5685

-micromass

Yes, that seems to work. But really what I need is what chogg stated. I need to able to "mask out", as chogg perfectly put it, more than one element. Will that work for picking out more than one element..? Thanks for the help.

-chogg
Yes, that is exactly what I need to do. You see I'm going to have one matrix([A]) the size of 680x480 which will have certain elements that are essentially non-zero and the rest will be zero. So basically, everywhere the elements are zero in [A], I need to change the values in another matrix , which has the same dimensions, to zero as well.

In the end, the third matrix [C] should have all the zeros from [A] but where it was non-zero in [A] to have whatever values from .

Maybe I remember it wrong and it isn't thorough matrix multiplication. Is there any matrix operations I can use to achieve my goal..?

-edit

I didn't see your last post chogg...

6. Jul 1, 2011

### Yondaime5685

Okay, after experimenting with your post, micromass, I found out it does work for any value you want to pick off. So I want to thank you for that. But I have to translate this into coding and it seems like it is going to be more code to write this than just my original idea to use an if and else to read off every element from one matrix and use it to pick off from the second matrix.

I just thought there was a simple matrix operation to get the same result. But, thanks to both micromass and chogg for helping me out.

7. Jul 1, 2011

### chogg

Ah, so it's a programming question. That makes it easy. :) Just define a mask -- a matrix of the same size as A with ones where you want to keep the elements and zeroes elsewhere. Then, instead of matrix multiplication, just do element-by-element multiplication.

I don't know what language you're using, but element-by-element multiplication is always easy to do. I usually use R, and it's the default multiplication of two matrices. :)

8. Jul 1, 2011

### Yondaime5685

-chogg

Yes. That helps. I'm using Matlab and remember seeing a code in the User's Guide that probably does the same thing. Thanks. :tongue2:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook