Thread: Matrix Multiplication

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Matrix Multiplication

    Am I correct? Because the program is not running correctly!

    Code:
    for(loop1 = 0; loop1 < no_of_rows1; loop1++)
       {
           for(loop2 = 0; loop2 < no_of_cols2; loop2++)
           {
                result[loop1][loop2] = 0;
                for(loop3 = 0; loop3 < no_of_cols1; loop3++)
                   result[loop1][loop2] += matrix1[loop1][loop3] * matrix2[loop3][loop2];
                printf("%d\t",result[loop1][loop2]);
           }
           printf("\n");
       }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %d worries me a little bit, but if your matrices are integers then that's fine. Otherwise the logic is correct.

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    I think your first (outer most) loop should be columns and the second loop should be rows. I think you're getting result = BA but you're expecting result = AB.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by Zlatko View Post
    I think your first (outer most) loop should be columns and the second loop should be rows. I think you're getting result = BA but you're expecting result = AB.
    I'm not sure about that, usually row comes first, but that depends on if its row-major or column-major.
    Spidey out!

  5. #5
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Quote Originally Posted by Spidey View Post
    I'm not sure about that, usually row comes first, but that depends on if its row-major or column-major.
    My mistake.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Spidey View Post
    I'm not sure about that, usually row comes first, but that depends on if its row-major or column-major.
    True, when you define your matrix; but in the code you are needing to navigate across the columns in A's matrix to multiply against the rows of matrix B.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by slingerland3g View Post
    True, when you define your matrix; but in the code you are needing to navigate across the columns in A's matrix to multiply against the rows of matrix B.
    Except, of course, that's exactly backwards. (The rows of the first matrix are multiplied agianst the columns of the second.)

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by slingerland3g View Post
    True, when you define your matrix; but in the code you are needing to navigate across the columns in A's matrix to multiply against the rows of matrix B.
    Nope, Rows of A against Colums of B.
    Spidey out!

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Ok, my logic is correct but definition was not clear. I meant that you multiply across your rows in matrix A and then down each column in matric B. My saving grace was I stated "Across the columns in Matric A". It was just my point of view. In other words multiply the first row vector in matrix A against the column vector of matrix B.
    Last edited by slingerland3g; 07-30-2009 at 03:30 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by slingerland3g View Post
    Ok, my logic is correct but definition was not clear. I meant that you multiply across your rows in matrix A and then down each column in matric B. My saving grace was I stated "Across the columns in Matric A". It was just my point of view. In other words multiply the first row vector in matrix A against the column vector of matrix B.
    And in other words, that's exactly what was posted. (The second index is changing for matrix A -- so we're keeping the row and moving from column to column; this is what we mean by "a row of A".)

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You cannot concatenate two arbitrary sized matrices together and expect a meaningful result. Your code will not work for all matrices yet attempts to give the illusion that it does.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Program works correctly for square and non-square matrices.
    Last edited by nonoob; 07-31-2009 at 09:52 AM. Reason: fix spelling

  13. #13

  14. #14
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Okay thanks a lot..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM