Java Matrix Multiplication with Source Code

Java Matrix Multiplication is a basic binary operation that combines the elements of two matrices into a single matrix.

When two order ab and bc matrices P and Q are multiplied, the resultant matrix is of order a*c.

In this section, we’ll go over the various techniques for multiplying two matrices in Java.

The compiler has been included so that you can run the programs yourself, with appropriate examples and outputs.

In this here, we use the For loop example.

Sample Code:

package javamatrixmultiplication;

import java.util.Scanner;

public class JavaMatrixMultiplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int r1, r2, c1, c2, i, j, k, sum;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the number of rows of matrix1");
        r1 = in.nextInt();

        System.out.println("Enter the number columns of matrix 1");
        c1 = in.nextInt();
        System.out.println("Enter the number of rows of matrix2");
        r2 = in.nextInt();

        System.out.println("Enter the number of columns of matrix 2");
        c2 = in.nextInt();

        if (c1 == r2) {

            int mat1[][] = new int[r1][c1];
            int mat2[][] = new int[r2][c2];
            int res[][] = new int[r1][c2];

            System.out.println("Enter the elements of matrix1");

            for (i = 0; i < r1; i++) {

                for (j = 0; j < c1; j++) {
                    mat1[i][j] = in.nextInt();
                }

            }
            System.out.println("Enter the elements of matrix2");

            for (i = 0; i < r2; i++) {

                for (j = 0; j < c2; j++) {
                    mat2[i][j] = in.nextInt();
                }

            }

            System.out.println("\n\noutput matrix:-");
            for (i = 0; i < r1; i++) {
                for (j = 0; j < c2; j++) {
                    sum = 0;
                    for (k = 0; k < r2; k++) {
                        sum += mat1[i][k] * mat2[k][j];

                    }
                    res[i][j] = sum;
                }
            }
            for (i = 0; i < r1; i++) {
                for (j = 0; j < c2; j++) {
                    System.out.print(res[i][j] + " ");
                }

                System.out.println();
            }
        } else {
            System.out.print("multipication does not exist ");
        }
    }
}

Sample Code Output:



Enter the number of rows of matrix1
3
Enter the number columns of matrix 1
3
Enter the number of rows of matrix2
3
Enter the number of columns of matrix 2
3
Enter the elements of matrix1
1 2 3
4 5 6
7 8 9
Enter the elements of matrix2
9 8 7
6 5 4
3 2 1


output matrix:-
30 24 18 
84 69 54 
138 114 90 

What is the importance of the Java Matrix Multiplication Program?

A matrix’s numbers can be used to represent both data and mathematical equations.

Multiplying matrices can provide quick but accurate approximations of considerably more difficult calculations in many time-sensitive engineering applications.

What is the matrix multiplication condition?

When did matrix multiplication become a thing?

The number of columns in the first matrix must equal the number of rows in the second matrix for matrix multiplication to be defined.

About the Project

Multiply the components of the first row of the first matrix with the first column of the second matrix, then add the product of the elements.

Repeat until each row of the first matrix has been multiplied by each column of the second matrix.

Project Details and Technology

Project Name:Java Matrix Multiplication with Source Code
AbstractThe Java Matrix Multiplication is a functional project which beginners can learn in this simple program that can enhance their programming skills.
Language/s Used:Java
Java version (Recommended):8
Database:N/A
Type:Desktop Application
Developer:sourcecodehero
Updates:0

To start executing this Java project, make sure that you have  NetBeans IDE or any platform of code editor installed on your computer.

You can also watch the tutorial in Java project just click here.

Procedure to run the system

Time needed: 3 minutes

  • Step 1: Download Source Code

    To get started, find the downloaded source code file below and click to start downloading it.

    Java Matrix Multiplication

  • Step 2: Extract File

    Navigate to the file’s location when you’ve finished downloading it and right-click it to extract it.

    Java Matrix Multiplication extract

  • Step 3: Run the project

    Next, open NetBeans IDE click open project, and choose your download.

    Java Matrix Multiplication open projects

  • Step 4: Run the project.

    Next, right-click the project folder and click run.

Download the Source Code below

Summary

For those Web developers interested in learning more about web apps, this system is a fully functional project.

Inquiries

If you have any questions or comments on the project, please leave a comment below.

Leave a Comment