Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Loop ordering in matrix multiplication

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop ordering in matrix multiplication

    Does it matter how the nested loop is ordered in a print method ( for example when multiplying matrices)..It seems that when I change the order of the for statements in the print method below, the multiplication is incorrect ( the resultant matrix is incorrect) ...Anyone have any idea? Maybe I have an error in my code??

    class Q2Code{
    static final int row1 = 6;
    static final int col1 = 6;
    static final int row2 = 6;
    static final int col2 = 6;
    public static void main (String args[]){
    int m1[][] = new int [row1][col1];
    int m2[][]= new int [row2][col2];
    int result[][]= new int [row1][col2];

    build(m1);
    build(m2);

    multiply(m1,m2,result);
    printMatrix(m1);
    System.out.println();
    printMatrix(m2);
    System.out.println();
    printMatrix(result);
    }
    // Create a new method - build - to initialise a matrix in a random manner
    static void build(int m1[][]){
    for(int j=0;j<m1.length;j++){
    for(int k=0; k<m1[0].length;k++){
    m1[j][k]=(int)(Math.random()*100)%5;
    }
    }
    }
    // Create a new method - multiply - to define how the resultant matrix will be calculated
    static void multiply(int m1[][],int m2[][],int res[][]){
    for(int k=0; k<res[0].length;k++){
    for(int j=0;j<res.length;j++){
    res[j][k] = product(m1,m2,j,k);
    }
    }
    }
    // Create a new method - product - that will specify how the operation of the multiplication will take place
    static int product(int m1[][],int m2[][], int row, int col){
    int sum=0;
    //Multiply rows in matrix 1 into columns of matrix 2
    for(int j=0;j<m2.length;j++){ //Note the useful guard as columns in matrix 1 must equal rows in matrix 2
    sum = sum + m1[row][j]* m2[j][col];
    }
    return sum;
    }
    // Create a new method - printMatrix - which defines how a matrix will be printed
    static void printMatrix(int m1[][]){
    for (int j=0; j<m1.length; j++){
    for (int k=0; k<m1[0].length;k++){
    System.out.printf("%4d",m1[j][k]);
    }
    System.out.println();
    }
    }

    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop ordering in matrix multiplication

    Let's have an example.
    int arr[][] = new int[10][5];
    for(int i =0;i<10;i++){
               for(int j=0;j<5;j++){
                     arr[i][j]=(i*j);
                      }
          }
    and........
    int arr[][] = new int[10][5];
    for(int i =0;i<5;i++){
               for(int j=0;j<10;j++){
                     arr[i][j]=(i*j);
                      }
          }

    What do you say the difference is?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop ordering in matrix multiplication

    Okay, I should have said does it matter in a square matrix like in my example of 6*6? If you switch the order of the "for" statements in the print method within the code, it gives different results (or a result that is incorrect)..Please advise??
    Last edited by murph; November 23rd, 2011 at 02:34 PM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop ordering in matrix multiplication

    If you will talk about Matrix multiplication, yes, it does matter. Coz
    123
    456
    789

    and

    987
    654
    321

    will give different results. (As far as Mathematics say) I am not that good in Mathematics but still it's a fact that by changing the order in multiplication, they will give different results.

Similar Threads

  1. Matrix multiplication problem with different dimensions
    By mightyking in forum Collections and Generics
    Replies: 5
    Last Post: September 25th, 2011, 04:59 PM
  2. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  3. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 02:04 AM
  4. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM
  5. Deciding Interceptor Ordering?
    By arpitgadle in forum Web Frameworks
    Replies: 1
    Last Post: August 10th, 2010, 05:33 AM