Sunday 20 January 2013

Matrix Multiplication in java




class MatrixMultiply{

        public static void main(String[]args)  {

          int array[][] ={{1,2,3},{4,5,6},{7,8,9}};

          int array1[][] ={{1,2,3},{4,5,6},{7,8,9}};

          int array2[][] = new int[3][3];

          int x= array.length;

          System.out.println("Matrix 1: ");

            for(int i = 0; i <x; i++) {

            for(int j = 0; j <x; j++) {

              System.out.print(" "+ array[i][j]);

            }

          System.out.println();

          }

          int y= array1.length;

          System.out.println("Matrix 2: ");

            for(int i = 0; i <y; i++) {

            for(int j = 0; j <y; j++) {

              System.out.print(" "+array1[i][j]);

            }

          System.out.println();

          }

            for(int i = 0; i <x; i++) {

            for(int j = 0; j <y; j++) {

              for(int k = 0; k <y; k++){

                array2[i][j] +=array[i][k]*array1[k][j];

              }

            }

           }

          System.out.println("Multiply of both matrix : ");

          for(int i = 0; i <x; i++) {

            for(int j = 0; j <y; j++) {

              System.out.print(" "+array2[i][j]);

            }

          System.out.println();

          }

        }

      }

No comments:

Post a Comment