Results 1 to 11 of 11
 07082013, 10:49 PM #1Member
 Join Date
 Oct 2012
 Posts
 35
 Rep Power
 0
Matrix multiplication program will not work properly. Printing result is wrong.
Hi! I have here a java code for a matrix multiplication. The code runs fine but the third column displays zeros down through it which should be 83 and 34, using the matrices in the program. Can somebody pinpoint where I am going wrong?? I have een trying for ages but I have no idea. Thank you!
Java Code:import javax.swing.JOptionPane; import java.util.Scanner; public class Test { public static void main(String[]args) { int[][]a = {{2,9},{7,3}}; int[][]b = {{3,4,1},{3,2,9}}; multiplyMatrix(a,b); } public static void multiplyMatrix(int[][] a, int[][] b) { int sum=0; int[][] result = new int[a.length][b[0].length]; if(a[0].length == b.length){ for(int i=0; i < a[0].length; i++){ for(int j=0; j < b.length; j++){ for (int k = 0 ; k < b.length ; k++ ) { sum += (a[i][k]*b[k][j]); } result[i][j] = sum; sum = 0; } } for(int i=0; i < result.length; i++){ for(int j=0; j < result[0].length; j++){ System.out.print(result[i][j] + " "); } System.out.println(); } } else System.out.print("That is not a valid multiplication as the row length of Matrix A is not equal to the column length of Matrix B"); } }
 07082013, 11:07 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,419
 Rep Power
 7
Re: Matrix multiplication program will not work properly. Printing result is wrong.
For one thing, your algorithm is wrong. b.length is 2 so you never get the third element of the b[x] array objects.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07092013, 12:17 AM #3Member
 Join Date
 Oct 2012
 Posts
 35
 Rep Power
 0
 07092013, 12:36 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,419
 Rep Power
 7
Re: Matrix multiplication program will not work properly. Printing result is wrong.
i goes to a.length
j goes to b[0].length
k goes to b.length
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07092013, 12:44 AM #5Member
 Join Date
 Oct 2012
 Posts
 35
 Rep Power
 0
Re: Matrix multiplication program will not work properly. Printing result is wrong.
Thanks so much Jim! :)
 07092013, 10:14 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,904
 Blog Entries
 7
 Rep Power
 22
Re: Matrix multiplication program will not work properly. Printing result is wrong.
Matrix multiplication always makes me dizzy and I always mix up the row and column indexes; I prefer to unravel the problem into a series of dot product operations: C= A*B is a series of dot products: the dot product of a row of A and a column of B. A dot product with the ith row of A and the jth column of B ends up to be element i, j of the product matrix C. Creating a separate little method for that dot product also removes one nasty nested loop and moves it into that method:
Java Code:for (int i= 0; i < A.length; i++) for (int j= 0; j < B[0].length; ++) C[i][j]= dotproduct(A[i], B, j);
Joscenosillicaphobia: the fear for an empty beer glass
 07092013, 05:07 PM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,419
 Rep Power
 7
Re: Matrix multiplication program will not work properly. Printing result is wrong.
Yep. In fact, I did the same thing in my example (personal example). But I just posted the fix to comply with the OP's algorithm. Actually, I really prefer to break down stuff like this with smaller, private method calls. It really helps make things clearer and facilitates debugging.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07092013, 05:32 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,904
 Blog Entries
 7
 Rep Power
 22
Re: Matrix multiplication program will not work properly. Printing result is wrong.
Indeed, and if performances becomes a bottle neck, one can always inline that dotproduct( ... ) method again (or even better: think of other solutions such as sparse matrices etc.)
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 07102013, 08:55 AM #9Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,702
 Rep Power
 6
Re: Matrix multiplication program will not work properly. Printing result is wrong.
Pretty sure the JVM optimizer already does that for you. That's the magic of Java; you write the dumb, simple code and the optimizer does the rest. Optimization attempts on the Java platform should not be focused on the code but rather the optimizer: how can you write the code such that the optimizer can work with it? Otherwise you'll likely just keep butting heads with the JVM.
What I'm trying to say is: be careful with recommendations about manual micro optimizations, I probably don't have to tell you that it just doesn't work the same way it does in a C/C++ application.
 07102013, 09:40 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,904
 Blog Entries
 7
 Rep Power
 22
Re: Matrix multiplication program will not work properly. Printing result is wrong.
All true; sometimes I fall for that trap, i.e. when there's nothing to refactor anymore (imho) I start microoptimizing the code and therefore ruining it ;) I did write "even better: think of other solutions ..." because performance (or accuracy) problems with these type of numerical problems are too dirty to handle with JIT optimization or simple refactoring ...
kind regards,
Jos
ps. the OP wants the product of two integer matrices so there won't be many accuracy problems here (except int overflow ;)cenosillicaphobia: the fear for an empty beer glass
 07102013, 02:36 PM #11Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,419
 Rep Power
 7
Re: Matrix multiplication program will not work properly. Printing result is wrong.
So let me understand something here. In C I recall you could have macros to aid inlining small bits of code. Are you saying that the Java optimizer will decide whether to take a method and inline it vs use a method call?
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

The <sup> tag doesn't work properly
By JosAH in forum AWT / SwingReplies: 26Last Post: 02182012, 08:55 AM 
NonSquare Matrix Multiplication
By j_arif123 in forum New To JavaReplies: 4Last Post: 01222012, 07:32 PM 
remove duplicates in array gives wrong result
By hacikho in forum New To JavaReplies: 1Last Post: 11252011, 04:31 AM 
Help in printing the result of the for loop
By maas in forum Java ServletReplies: 1Last Post: 05122011, 09:30 AM 
Implement a multithreaded matrix multiplication program.
By redasu in forum Threads and SynchronizationReplies: 0Last Post: 11172010, 10:00 AM
Bookmarks