🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Matrix multiplication is a fundamental operation in linear algebra that takes a pair of matrices and produces another matrix. In this tutorial, we will write a Java program to perform matrix multiplication.
2. Program Steps
1. Define a class named MatrixMultiplication.
2. Inside the main method, define two 2D arrays matrix1 and matrix2 representing the matrices to be multiplied.
3. Check if the number of columns in matrix1 is equal to the number of rows in matrix2. If not, matrix multiplication is not possible.
4. If multiplication is possible, define a 2D array result to store the multiplication result.
5. Use nested for loops to calculate the elements of the result matrix by summing the products of corresponding elements of matrix1 and matrix2.
6. Print the result matrix.
3. Code Program
public class MatrixMultiplication { // Step 1: Define a class named MatrixMultiplication
public static void main(String[] args) { // Main method
// Step 2: Define two 2D arrays matrix1 and matrix2
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
// Step 3: Check if the number of columns in matrix1 is equal to the number of rows in matrix2
if (matrix1[0].length != matrix2.length) {
System.out.println("Matrix multiplication is not possible.");
} else {
// Step 4: Define a 2D array result to store the multiplication result
int rows = matrix1.length;
int columns = matrix2[0].length;
int[][] result = new int[rows][columns];
// Step 5: Use nested for loops to calculate the elements of the result matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
for (int k = 0; k < matrix2.length; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Step 6: Print the result matrix
System.out.println("Multiplication of the matrices:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
}
Output:
Multiplication of the matrices: 30 24 18 84 69 54 138 114 90
4. Step By Step Explanation
Step 1: A class named MatrixMultiplication is defined.
Step 2: Two 2D arrays matrix1 and matrix2 representing the matrices to be multiplied are defined inside the main method.
Step 3: The program checks whether the number of columns in matrix1 is equal to the number of rows in matrix2, as this condition is necessary for matrix multiplication. If not, it prints a message and exits.
Step 4: If matrix multiplication is possible, a 2D array result is defined to store the multiplication result.
Step 5: Nested for loops are used to calculate the elements of the result matrix by summing the products of corresponding elements of matrix1 and matrix2.
Step 6: The program prints the result matrix, which is the product of matrix1 and matrix2.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment