Comments

  1. Hassan:

    Very helpful.
    Thanks

    October 22, 2010 at 5:21 pm |
  2. Kevin:

    Thanks for this, I appreciate it! Matrices are not fun and my problem was I wasn’t sure if I did need another for loop or two but I had the first 2 loops lol.

    December 13, 2010 at 6:46 pm |
  3. SSU:

    Thanks alot Ryan,

    I am really glad of the code. I am terrible at manual matrix calculations and always get them wrong.

    February 3, 2011 at 8:08 pm |
  4. Nobody:

    Thanks a lot. Really cleared up some doubts.

    March 15, 2011 at 4:45 am |
  5. sreeprasad:

    The second matrix’s number of columns: int bRows = b[0].length;

    This should be

    The second matrix’s number of columns: int bColumns = b[0].length;

    April 30, 2011 at 2:59 pm |
  6. Kristian Vestergaard:

    Hail! Hail!

    You saved my day! I was lost in loop that looped in a loop because of the first loop….

    :)

    May 13, 2011 at 3:35 am |
  7. Chris:

    The line:

    throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
    

    should probably read:

    throw new IllegalArgumentException("A:Columns: " + aColumns + " did not match B:Rows " + bRows + ".");
    
    May 25, 2011 at 2:26 am |
  8. bunrab:

    Nice!!!

    June 19, 2011 at 7:44 pm |
  9. Jeremy:

    If you are multiplying matrices which have square dimensions or matrix a has more columns than rows this code works fine. However, if it has more rows than columns this will not produce the correct result. You might try:

    int[][] resultant;
    if(aColumns >= aRows){
       resultant = new int[aRows][bColumns];
    } else{
       resultant = new int[aColumns][bRows];
    }
    

    Also, your error checking should include a statement comparing aRows to bColumns thusly:

    if(aColumns != bRows || aRows != bColumns)
    

    Just a couple fixes to increase the range of your code.

    Merged
    Sorry, I misunderstood matrix multiplication. Your assignment to resultant is correct, however my addendum to your error checking code is still correct.

    October 23, 2011 at 1:56 pm |
  10. qwerty99:

    thank you, this was very helpfull for me :)

    November 6, 2011 at 8:20 pm |
  11. Michael:

    The only problem I am having now is how to print the 2D resultant array we get. When I just try to print the resultant array I get a string of letters, numbers, and symbols.

    February 17, 2012 at 2:45 pm |
  12. Ali:

    Try this input it will generate result, where as input is not correct, it has to show Exception

    int[][] tab1 = {{1,2},{1,2},{1,2}};
    int[][] tab2 = {{1,3},{1,4,3,4,5,6}};

    March 28, 2012 at 2:12 pm |
  13. You might be interested in my Blogpost: http://martin-thoma.com/matrix-multiplication-python-java-cpp/

    June 22, 2012 at 4:05 am |

Leave a Reply

You must be logged in to post a comment.