Java Matrix Operations

  1. Matrix (Two Dimensional Array) Creation in Java
  2. Matrix Addition in Java
  3. Matrix Subtraction in Java
  4. Matrix Multiplication in Java
  5. Matrix Division in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Matrix (Two Dimensional Array) Creation in Java


Matrix_Create.java

import java.util.Scanner;
 
public class Matrix_Create {
	
	Scanner scan;
	int matrix[][];
	int row, column;
 
	void create() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Matrix Creation");
		
		System.out.println("\nEnter number of rows :");
		row = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter number of columns :");
		column = Integer.parseInt(scan.nextLine());
		
		matrix = new int[row][column];
		System.out.println("Enter the data :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix[i][j] = scan.nextInt();
			}
		}
	}
	
	void display() {
		
		System.out.println("\nThe Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix[i][j]);
			}
			System.out.println();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Matrix_Create obj = new Matrix_Create();
		
		obj.create();
		obj.display();
	}
}

Sample Output

Matrix Creation

Enter number of rows :
3
Enter number of columns :
3
Enter the data :
1 2 3 4 5 6 7 8 9

The Matrix is :
	1	2	3
	4	5	6
	7	8	9




B. Matrix Addition in Java


Matrix_Addition.java

import java.util.Scanner;
 
public class Matrix_Addition {
	
	Scanner scan;
	int matrix1[][], matrix2[][], sum[][];
	int row, column;
 
	void create() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Matrix Addition");
		
		// First Matrix Creation..
		System.out.println("\nEnter number of rows & columns");
		row = Integer.parseInt(scan.nextLine());
		column = Integer.parseInt(scan.nextLine());
		
		matrix1 = new int[row][column];
		matrix2 = new int[row][column];
		sum = new int[row][column];
 
		System.out.println("Enter the data for first matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix1[i][j] = scan.nextInt();
			}
		}
		
		// Second Matrix Creation..
		System.out.println("Enter the data for second matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix2[i][j] = scan.nextInt();
			}
		}
	}
	
	void display() {
		
		System.out.println("\nThe First Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix1[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("\n\nThe Second Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix2[i][j]);
			}
			System.out.println();
		}
	}
	
	void add() {
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				sum[i][j] = matrix1[i][j] + matrix2[i][j];
			}
		}
		
		System.out.println("\n\nThe Sum is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + sum[i][j]);
			}
			System.out.println();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Matrix_Addition obj = new Matrix_Addition();
		
		obj.create();
		obj.display();
		obj.add();
	}
}

Sample Output

Matrix Addition

Enter number of rows & columns
2
2
Enter the data for first matrix :
1 2 3 4
Enter the data for second matrix :
5 6 7 8

The First Matrix is :
	1	2
	3	4


The Second Matrix is :
	5	6
	7	8


The Sum is :
	6	8
	10	12




C. Matrix Subtraction in Java


Matrix_Subtraction.java

import java.util.Scanner;
 
public class Matrix_Subtraction {
	
	Scanner scan;
	int matrix1[][], matrix2[][], sub[][];
	int row, column;
 
	void create() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Matrix Subtraction");
		
		// First Matrix Creation..
		System.out.println("\nEnter number of rows & columns");
		row = Integer.parseInt(scan.nextLine());
		column = Integer.parseInt(scan.nextLine());
		
		matrix1 = new int[row][column];
		matrix2 = new int[row][column];
		sub = new int[row][column];
 
		System.out.println("Enter the data for first matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix1[i][j] = scan.nextInt();
			}
		}
		
		// Second Matrix Creation..
		System.out.println("Enter the data for second matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix2[i][j] = scan.nextInt();
			}
		}
	}
	
	void display() {
		
		System.out.println("\nThe First Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix1[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("\n\nThe Second Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix2[i][j]);
			}
			System.out.println();
		}
	}
	
	void sub() {
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				sub[i][j] = matrix1[i][j] - matrix2[i][j];
			}
		}
		
		System.out.println("\n\nThe Subtraction is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + sub[i][j]);
			}
			System.out.println();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Matrix_Subtraction obj = new Matrix_Subtraction();
		
		obj.create();
		obj.display();
		obj.sub();
	}
}

Sample Output

Matrix Subtraction

Enter number of rows & columns
2
2
Enter the data for first matrix :
9 8 7 6
Enter the data for second matrix :
5 4 3 2

The First Matrix is :
	9	8
	7	6


The Second Matrix is :
	5	4
	3	2


The Subtraction is :
	4	4
	4	4




D. Matrix Multiplication in Java


Matrix_Multiplication.java

import java.util.Scanner;
 
public class Matrix_Multiplication {
	
	Scanner scan;
	int matrix1[][], matrix2[][], multi[][];
	int row, column;
 
	void create() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Matrix Multiplication");
		
		// First Matrix Creation..
		System.out.println("\nEnter number of rows & columns");
		row = Integer.parseInt(scan.nextLine());
		column = Integer.parseInt(scan.nextLine());
		
		matrix1 = new int[row][column];
		matrix2 = new int[row][column];
		multi = new int[row][column];
 
		System.out.println("Enter the data for first matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix1[i][j] = scan.nextInt();
			}
		}
		
		// Second Matrix Creation..
		System.out.println("Enter the data for second matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix2[i][j] = scan.nextInt();
			}
		}
	}
	
	void display() {
		
		System.out.println("\nThe First Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix1[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("\n\nThe Second Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix2[i][j]);
			}
			System.out.println();
		}
	}
	
	void multi() {
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				multi[i][j] = matrix1[i][j] * matrix2[i][j];
			}
		}
		
		System.out.println("\n\nThe Multiplication is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + multi[i][j]);
			}
			System.out.println();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Matrix_Multiplication obj = new Matrix_Multiplication();
		
		obj.create();
		obj.display();
		obj.multi();
	}
}

Sample Output

Matrix Multiplication

Enter number of rows & columns
2
2
Enter the data for first matrix :
1 2 3 4
Enter the data for second matrix :
5 6 7 8

The First Matrix is :
	1	2
	3	4


The Second Matrix is :
	5	6
	7	8


The Multiplication is :
	5	12
	21	32




E. Matrix Division in Java


Matrix_Division.java

import java.util.Scanner;
 
public class Matrix_Division {
	
	Scanner scan;
	int matrix1[][], matrix2[][], div[][];
	int row, column;
 
	void create() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Matrix Division");
		
		// First Matrix Creation..
		System.out.println("\nEnter number of rows & columns");
		row = Integer.parseInt(scan.nextLine());
		column = Integer.parseInt(scan.nextLine());
		
		matrix1 = new int[row][column];
		matrix2 = new int[row][column];
		div = new int[row][column];
 
		System.out.println("Enter the data for first matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix1[i][j] = scan.nextInt();
			}
		}
		
		// Second Matrix Creation..
		System.out.println("Enter the data for second matrix :");
 
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				matrix2[i][j] = scan.nextInt();
			}
		}
	}
	
	void display() {
		
		System.out.println("\nThe First Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix1[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("\n\nThe Second Matrix is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + matrix2[i][j]);
			}
			System.out.println();
		}
	}
	
	void div() {
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				div[i][j] = matrix1[i][j] / matrix2[i][j];
			}
		}
		
		System.out.println("\n\nThe Division is :");
		
		for(int i=0; i<row; i++) {
			
			for(int j=0; j<column; j++) {
				
				System.out.print("\t" + div[i][j]);
			}
			System.out.println();
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Matrix_Division obj = new Matrix_Division();
		
		obj.create();
		obj.display();
		obj.div();
	}
}

Sample Output

Matrix Division

Enter number of rows & columns
2
2
Enter the data for first matrix :
8 8 8 8
Enter the data for second matrix :
4 4 4 4

The First Matrix is :
	8	8
	8	8


The Second Matrix is :
	4	4
	4	4


The Division is :
	2	2
	2	2






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com