Introduction
Matrix Multiplication is one of the most important operations performed on matrices in Java programming. It is used to multiply two matrices by calculating the sum of products of corresponding elements from rows of the first matrix and columns of the second matrix.
In Java, matrices are represented using two-dimensional arrays. Matrix multiplication helps programmers understand advanced concepts such as nested loops, arrays, mathematical operations, and data processing.
Matrix multiplication is widely used in:
- Computer graphics
- Machine learning
- Artificial intelligence
- Scientific calculations
- Image processing
- Engineering applications
- Data analysis
For example:
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
Result Matrix:
43 50
The calculation works as:
First element:
(1 × 5) + (2 × 7)
= 5 + 14
= 19
In this tutorial, you will learn how to perform matrix multiplication in Java, understand the algorithm, write Java programs using arrays and user input, analyze complexity, learn common mistakes, and explore interview questions.
What is Matrix Multiplication?
Matrix Multiplication is a mathematical operation where two matrices are multiplied to produce a new matrix.
If:
- Matrix A has dimensions m × n
- Matrix B has dimensions n × p
Then the resulting matrix C will have dimensions:
m × p
The formula for matrix multiplication is:
C[i][j] = C[i][j] + A[i][k] × B[k][j]
Where:
- A = First matrix
- B = Second matrix
- C = Result matrix
- i = Row index
- j = Column index
- k = Common dimension
Rules for Matrix Multiplication
Before multiplying two matrices, the following condition must be satisfied:
Number of Columns of First Matrix Must Equal Number of Rows of Second Matrix
Example:
Valid multiplication:
Matrix A:
2 × 3
Matrix B:
3 × 4
Result:
Invalid multiplication:
Matrix A:
2 × 3
Matrix B:
2 × 4
Because, Columns of A ≠ Rows of B.
Syntax
The general syntax for matrix multiplication in Java:
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < columns2; j++) {
for(int k = 0; k < columns1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
Matrix multiplication requires three nested loops because every element of the first matrix must be multiplied with every element of the second matrix.
Java Matrix Multiplication Program
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2},
{3, 4}
};
int[][] matrix2 = {
{5, 6},
{7, 8}
};
int[][] result = new int[2][2];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Result Matrix:");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
Output
Result Matrix:
43 50
Explanation of the Program
Step 1: Create First Matrix
int[][] matrix1 = {
{1,2},
{3,4}
};
This creates the first matrix.
Step 2: Create Second Matrix
int[][] matrix2 = {
{5,6},
{7,8}
};
This creates the second matrix.
Step 3: Create Result Matrix
int[][] result = new int[2][2];
This matrix stores the multiplication result.
Step 4: Multiply Matrix Elements
result[i][j] += matrix1[i][k] * matrix2[k][j];
This statement multiplies elements and adds their values.
Example:
result[0][0]
= (1×5) + (2×7)
= 5 + 14
= 19
How Matrix Multiplication Works?
Consider two matrices:
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
First Element
Row 1 of A × Column 1 of B
(1×5) + (2×7)
= 19
Second Element
Row 1 of A × Column 2 of B
(1×6) + (2×8)
= 22
Third Element
Row 2 of A × Column 1 of B
(3×5) + (4×7)
= 43
Fourth Element
Row 2 of A × Column 2 of B
(3×6) + (4×8)
= 50
Final result:
43 50
Java Matrix Multiplication Using User Input
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows of first matrix: ");
int rows1 = scanner.nextInt();
System.out.print("Enter columns of first matrix: ");
int columns1 = scanner.nextInt();
System.out.print("Enter rows of second matrix: ");
int rows2 = scanner.nextInt();
System.out.print("Enter columns of second matrix: ");
int columns2 = scanner.nextInt();
if(columns1 != rows2) {
System.out.println("Matrix multiplication is not possible.");
scanner.close();
return;
}
int[][] matrix1 = new int[rows1][columns1];
int[][] matrix2 = new int[rows2][columns2];
int[][] result = new int[rows1][columns2];
System.out.println("Enter first matrix elements:");
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < columns1; j++) {
matrix1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter second matrix elements:");
for(int i = 0; i < rows2; i++) {
for(int j = 0; j < columns2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < columns2; j++) {
for(int k = 0; k < columns1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Result Matrix:");
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < columns2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}
Output
2
Enter columns of first matrix:
2
Enter rows of second matrix:
2
Enter columns of second matrix:
2
Enter first matrix elements:
1
2
3
4
Enter second matrix elements:
5
6
7
8
Result Matrix:
19 22
43 50
Algorithm for Matrix Multiplication
- Start the program.
- Read dimensions of both matrices.
- Check whether multiplication is possible.
- Create a result matrix.
- Use three nested loops.
- Multiply corresponding elements.
- Add the multiplied values.
- Store the result.
- Display the result matrix.
- End the program.
Flowchart
Start
|
|
Read Two Matrices
|
|
Check Matrix Condition
|
|
Columns of First = Rows of Second?
|
Yes No
| |
Multiply Invalid
Matrices Operation
|
Store Result
|
Display Matrix
|
End
Time Complexity
For matrix multiplication:
- First matrix size = m × n
- Second matrix size = n × p
Time complexity:
O(m × n × p)
For two square matrices of size n × n:
O(n³)
because three loops are executed.
Space Complexity
O(m × p)
because an additional result matrix is created.
Advantages of Matrix Multiplication
- Helps solve mathematical problems efficiently.
- Used in scientific and engineering applications.
- Important for machine learning algorithms.
- Useful in computer graphics.
- Improves understanding of multidimensional arrays.
Disadvantages of Matrix Multiplication
- Requires compatible matrix dimensions.
- Uses more memory for large matrices.
- Computationally expensive for very large matrices.
- Requires three nested loops.
Matrix Addition vs Matrix Multiplication
| Feature | Matrix Addition | Matrix Multiplication |
|---|---|---|
| Operation | Add elements | Multiply and add elements |
| Loops Required | Two loops | Three loops |
| Condition | Same dimensions | Columns of first = Rows of second |
| Complexity | O(m × n) | O(m × n × p) |
| Result Size | Same as matrices | Depends on dimensions |
Common Mistakes
1. Ignoring Matrix Dimension Rules
Incorrect:
2 × 3 Matrix × 2 × 4 Matrix
This multiplication is not possible.
2. Using Only Two Loops
Incorrect:
for(i)
{
for(j)
{
}
}
Matrix multiplication requires three loops.
3. Forgetting to Initialize Result Matrix
Incorrect:
result[i][j] = value;
Correct:
result[i][j] += value;
4. Wrong Index Usage
Incorrect:
matrix1[i][j] * matrix2[i][j]
Correct:
matrix1[i][k] * matrix2[k][j]
5. Not Checking Compatibility
Always check:
columns of first matrix == rows of second matrix
Real-Life Applications of Matrix Multiplication
Matrix multiplication is used in:
- Artificial Intelligence
- Machine Learning
- Neural Networks
- Computer Graphics
- Image Processing
- Robotics
- Physics simulations
- 3D game development
- Data science
Examples:
- Transforming 3D objects in games.
- Processing images using pixels.
- Calculating neural network outputs.
Summary
The Java Matrix Multiplication Program is an important programming exercise that helps developers understand two-dimensional arrays, nested loops, and mathematical operations.
Matrix multiplication is more complex than matrix addition because each element of the result matrix requires multiplying and adding multiple values from rows and columns.
Java provides a simple way to implement matrix multiplication using arrays and loops. Learning this concept builds a strong foundation for advanced areas like machine learning, artificial intelligence, computer graphics, and scientific computing.