Matrix Multiplication Java Program

This post shows a Java program to multiply two matrices.

To multiply one matrix with another you need to do a dot product of rows and columns. Let’s see it with an example where you are trying to multiply a 3X3 matrix with a 3X2 matrix.

matrix multiplication Java program

Matrix Multiplication

How matrix multiplication happens here using dot product can be explained as follows-

First row of first matrix is multiplied with the first column of second matrix.

s11 = r11Xp11 + r12Xp21 + r13Xp31

Second row of first matrix is multiplied with the second column of second matrix.

s12 = r11Xp12 + r12Xp22 + r13Xp32

Then second row of first matrix is multiplied with the first column of second matrix.

s21 = r21Xp11 + r22Xp21 + r23Xp31

and so on…

Java program for matrix multiplication

In the matrix multiplication Java program, initially user is prompted to enter the matrices. You can also check that the number of columns in the first matrix are equal to the number of rows in the second matrix. Then using these two matrices you can do the multiplication.

Output

That’s all for the topic Matrix Multiplication Java Program. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.