Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Quick help needed on a Matrix Multiplication

  1. Jul 1, 2011 #1
    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. jcsd
  3. Jul 1, 2011 #2
    Hi Yondaime5685! :smile:

    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


    For example

    [tex]\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)[/tex]

    Can you such a reasoning to solve your problem?
    Last edited: Jul 1, 2011
  4. Jul 1, 2011 #3
    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 [itex]m \times n[/itex] matrix ("[itex]A[/itex]"), multiply it with other matrices, and get back another [itex]m \times n[/itex] matrix ("[itex]B[/itex]"). 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 [itex]M[/itex] is an [itex]m \times m[/itex] matrix, and [itex]N[/itex] is an [itex]n \times n[/itex] matrix. Your goal: the entries of [itex]B[/itex] should be either the same as the corresponding entries in [itex]A[/itex], or zero... and this should hold for any values of [itex]A[/itex].

    Does that sound right?

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

    So, it seems to me you can't mask out components of matrices by matrix multiplication.
  5. Jul 1, 2011 #4
    Silly me! Of course you can make it work by setting, for instance, [itex]M_{ik} = 0[/itex] for all k. This will mask out the entire i'th row of B.

    Alternatively, setting [itex]N_{lj}=0[/itex] 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 :)
  6. Jul 1, 2011 #5

    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.

    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..?


    I didn't see your last post chogg...
  7. Jul 1, 2011 #6
    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. :smile:
  8. Jul 1, 2011 #7
    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. :)
  9. Jul 1, 2011 #8

    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:
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook