Matrix multiplication is a fundamental operation in linear algebra and is widely used in various applications, including computer graphics, machine learning, and data analysis. In this article, we will explore how to perform matrix multiplication in Java using nested loops. We will also discuss the conditions required for matrix multiplication to be valid and provide a complete code example.
Matrix multiplication involves multiplying two matrices to produce a third matrix. The number of columns in the first matrix must equal the number of rows in the second matrix for multiplication to be possible. If matrix A has dimensions m×n and matrix B has dimensions n×p, the resulting matrix C will have dimensions m×p.
Two matrices can be multiplied only if the number of columns in the first matrix matches the number of rows in the second matrix.
Here’s a complete Java program that demonstrates how to perform matrix multiplication:
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input dimensions for first matrix
System.out.print("Enter number of rows for first matrix: ");
int rowsA = scanner.nextInt();
System.out.print("Enter number of columns for first matrix: ");
int colsA = scanner.nextInt();
// Input dimensions for second matrix
System.out.print("Enter number of rows for second matrix: ");
int rowsB = scanner.nextInt();
System.out.print("Enter number of columns for second matrix: ");
int colsB = scanner.nextInt();
// Check if multiplication is possible
if (colsA != rowsB) {
System.out.println("Matrix multiplication not possible.");
return;
}
// Initialize matrices
int[][] A = new int[rowsA][colsA];
int[][] B = new int[rowsB][colsB];
int[][] C = new int[rowsA][colsB];
// Input elements for first matrix
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
A[i][j] = scanner.nextInt();
}
}
// Input elements for second matrix
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
B[i][j] = scanner.nextInt();
}
}
// Perform matrix multiplication
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
C[i][j] = 0; // Initialize result cell
for (int k = 0; k < colsA; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Output result
System.out.println("Resultant Matrix:");
printMatrix(C);
}
// Method to print a matrix
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
Explanation of the Code
Example Output
When you run this program, you might see output similar to this:
Matrix multiplication is an essential operation in many fields, including computer science and engineering. This Java program provides a straightforward approach to performing this operation using nested loops. By understanding how to implement this functionality, beginners can strengthen their programming skills while gaining insight into mathematical operations in programming.