Wednesday, 21 June 2017

Matrix Addition - Java Program

When you add two matrices addition is done index wise you add the element at (0, 0) in the first matrix with the element at (0, 0) in the second matrix, element at (0, 1) in the first matrix with the element at (0, 1) in the second matrix and so on.

As example – If you are adding two matrices of order 3X3

Thus the resultant matrix is -

Also remember these points when adding one matrix with another -

  1. Both of the matrix have to be of same size.
  2. Resultant matrix will also have the same order for the elements. Element at (0, 0) in the first matrix added with (0, 0) of the second matrix becomes the element at index (0, 0) in the resultant matrix too.

Matrix addition Java program

 
import java.util.Scanner;

public class MatrixAddition {

    public static void main(String[] args) {
        int rowM, colM;
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter Number of Rows and Columns of Matrix : ");
        rowM = in.nextInt();
        colM = in.nextInt();
        
        int M1[][] = new int[rowM][colM];
        int M2[][] = new int[rowM][colM];
        int resMatrix[][] = new int[rowM][colM];
        
        System.out.print("Enter elements of First Matrix : ");
        
        for(int i = 0; i < rowM; i++){
            for(int j = 0; j < colM; j++){
                M1[i][j] = in.nextInt();
            }
        }
        System.out.println("First Matrix : " );
        for(int i = 0; i < rowM; i++){
            for(int j = 0; j < colM; j++){
                System.out.print(" " +M1[i][j]+"\t");
            }
            System.out.println();
        }
        
        System.out.print("Enter elements of Second Matrix : ");
        
        for(int i = 0; i < rowM; i++){
            for(int j = 0; j < colM; j++){
                M2[i][j] = in.nextInt();
            }
        }
        System.out.println("Second Matrix : " );
        for(int i = 0; i < rowM; i++){
            for(int j = 0; j < colM; j++){
                System.out.print(" " +M2[i][j] + "\t");
            }
            System.out.println();
        }
        
        // Addition logic 
        for(int i = 0; i < rowM; i++){
            for(int j = 0; j < colM; j++){
                resMatrix[i][j] = M1[i][j] + M2[i][j];
            }
        }
        
        // Printing the result matrix 
        System.out.println("Result Matrix : " );
        for(int i = 0; i < resMatrix.length; i++){
            for(int j = 0; j < colM; j++){
                System.out.print(" " +resMatrix[i][j]+"\t");
            }
            System.out.println();
        }

    }

}

Output

Enter Number of Rows and Columns of Matrix :  3 3

Enter elements of First Matrix : 1 3 4 2 5 6 4 3 2

First Matrix : 
 1  3  4 
 2  5  6 
 4  3  2
 
Enter elements of Second Matrix : 2 7 1 0 4 6 9 8 1

Second Matrix : 
 2  7  1 
 0  4  6 
 9  8  1

Result Matrix : 
 3  10  5 
 2  9  12 
 13  11  3 

That's all for this topic Matrix Addition - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Matrix Multiplication Java Program
  2. Array in Java
  3. How to remove duplicate elements from an array - Java Program
  4. How to swap or exchange two numbers without using any temporary variable - Java Program

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment