Sunday 7 September 2014

Matrix Multiplication Program in Java



Matrix Multiplication Program :


To multiply two matrices we need to first check whether the two matrices can be multiplied or not. For that compare the first matrix column with the rows of second matrix, if they are equal than only the two matrices can be multiplied with each other.

For example :

Let the first matrix be of the order 3X3 and second matrix be of the order of 3X2. Since both the no. of columns in first matrix and no. of rows in second matrix are equal i.e. 3, the two matrices can be multiplied with each other and the resultant matrix will be of the order 3X2 (rows in first matrix and columns in second matrix).

In general,
M(ixj) * M(jxk) = M(ixk)

To multiply the two matrices we need to multiply the rows of first matrix to the columns of second matrix as shown below in the diagram.



Now let’s consider the program of matrix multiplication in which the two matrices are multiplied with the use of three nested loops.

 import java.util.Scanner;  
   
 class MatrixMulDemo  
 {  
      public static void main(String[] args)  
      {  
           //make a scanner for rows and columns  
           Scanner scan = new Scanner(System.in);  
   
           System.out.println("Enter the number of rows for first matrix");  
   
           //store rows in a variable  
           int rowsFirst = scan.nextInt();  
           System.out.println("Enter the number of columns for first matrix / rows of   
   
 second matrix (they must be same for matrix multiplication");  
             
           //store columns in a variable  
           int colFirstRowSec = scan.nextInt();  
   
           System.out.println("Enter the columns for second matrix");  
           //store columns of second matrix in a variable  
           int colSec = scan.nextInt();  
   
           //make two dimensional arrays for storing the matrices elements  
   
           int[][] first = new int[rowsFirst][colFirstRowSec];  
           int[][] second = new int[colFirstRowSec][colSec];  
   
           System.out.println("Enter the values of first matrix");  
           for(int i=0; i<first.length; i++)  
           {  
                for(int j=0; j<first[0].length; j++)  
                {  
                     first[i][j] = scan.nextInt();  
                }  
           }  
   
           System.out.println("Enter the values of second matrix");  
           for(int i=0; i<second.length; i++)  
           {  
                for(int j=0; j<second[0].length; j++)  
                {  
                     second[i][j] = scan.nextInt();  
                }  
           }  
   
           //define the third resultant matrix  
           int result[][] = new int[rowsFirst][colSec];  
   
           //multiply the two matrices using three nested loops  
           for(int i=0; i<rowsFirst; i++)  
           {  
                for(int j=0; j<colSec; j++)  
                {  
                     for(int k=0; k<colFirstRowSec; k++)  
                     {  
                          result[i][j] = result[i][j] + first[i][k] * second  
   
 [k][j];  
                     }  
                }  
           }  
   
           //print all the elements of result matrix  
           System.out.println("Product of two matrices ");  
           for(int i=0; i<result.length; i++)  
           {  
                for(int j=0; j<result[0].length; j++)  
                {  
                     System.out.print(result[i][j] + " ");  
                }  
                //for line gap  
                System.out.println();  
           }  
      }  
 }  

Output :

29 comments :

  1. Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…JAVA Course in Chennai

    ReplyDelete
  2. thank you for your bolg with unique content.this is one of the recommanded blog.we appriciate your efforts.

    JAVA Adavanced Online training
    JAVA Adavanced Online training in hyderabad

    ReplyDelete
  3. After completing my basic core java knowldege, I have continued with this site. However, you should keep programming so simple for beginner as Java Online Tutorial has.

    ReplyDelete
  4. Thank you for sharing such conceptual blog.
    Find more details on Java Training

    ReplyDelete
  5. Java program is having expanding number of Job opportunities
    Find best Java training institute in Delhi

    ReplyDelete

  6. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks.
    UNIX Shell scripting training in chennai
    ORACLE apps finance training in chennai
    Informatica Online Training

    ReplyDelete
  7. CIIT Noida provides Best MCA Courses in Noida based on the current

    IT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad.

    CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced

    level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.

    At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live

    projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to

    students that transform an average student into a corporate professional whom companies prefer to hire.

    Best MCA College in Noida

    ReplyDelete
  8. Java training in Noida and Java training in Delhi delivered by CIITN.we tend to gives it making prepared in lightweight of corporates standards that helps understudies to be prepare for endeavors. CIITN offers best Java training in Noida, CIITN is one among the most straightforward result arranged Java training establishment in Noida, offers best for all purposes and capacities, test data in Java training in Noida. CIITN Java training is coordinated by 15+ years of inclusion in supervising steady ventures. CIITN Noida, is giving basic and impelled level of Java training with live tasks with 100% job oriented plan encourage with top MNC.

    Java Training in Noida

    best java training in noida


    ReplyDelete
  9. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  10. Great blog, thanks for sharing information with us!!!!
    Zenitech is best java training institute in Delhi. Our trainers realize what is required in Industry and help you learn and take a shot at International Standards.

    ReplyDelete
  11. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website
    Authorized iphone service center in Chennai | iphone service center in chennai | Mobile service center in chennai | Authorized iphone service center in Chennai | iphone service center in chennai

    ReplyDelete
  12. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  13. My brother suggested I might like this website. He was totally right. This post truly made my day. You can not imagine just how much time I had spent for this info! Thanks! imac computer berlin

    ReplyDelete
  14. nice article keep update. Thanks for such a great post

    """Dream Teach Provides Best Teaching and Learning resources of geography in UK. It provides info on a geographical environmental quiz, journals that are helpful for Primary & Secondary Schools Teachers and Students.""

    To know more about DreamTeach

    Visit: geography learning resources!

    ReplyDelete

  15. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete
  16. The blog effectively explains the process of matrix multiplication and provides a clear understanding of the topic. It serves as a helpful guide for anyone looking to learn or refresh their knowledge on matrix multiplication. Keep up the good work!
    To know more about java program, I recommend Java Certification Course in Indore as a best place for learning.

    ReplyDelete
  17. Thank you for sharing such conceptual blog. So informative blog.
    Best Data science Training in Kolkata

    ReplyDelete
  18. If you're eager to embark on a comprehensive journey into the world of Java Full Stack development, look no further than APTRON's Java Full Stack Course in Noida. This dynamic and industry-focused program is designed to equip you with the skills and knowledge necessary to excel in today's competitive tech landscape. Noida, with its burgeoning IT sector, provides an ideal backdrop for honing your Full Stack expertise.

    ReplyDelete