Addition of Two Matrix in Java: Step-by-Step Guide with Code Example

Matrix addition is the process of adding corresponding elements from two matrices of the same dimensions. For instance, given two 3x3 matrices, each element in the resulting matrix is the sum of the corresponding elements from the input matrices.

When working with Java, performing the addition of two matrix in Java is a common task, especially when dealing with multi-dimensional arrays. This guide will explain how to achieve the addition of two matrix in Java efficiently.

What is Matrix Addition?

Matrix addition is the process of adding corresponding elements from two matrices of the same dimensions. For instance, given two 3x3 matrices, each element in the resulting matrix is the sum of the corresponding elements from the input matrices.

Java Code Example for Addition of Two Matrices

Here's a simple Java program to demonstrate the addition of two matrix in Java using multi-dimensional arrays:

java
import java.util.Scanner;public class MatrixAddition { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Matrix dimensions int rows = 3, cols = 3; // Matrix declaration int[][] matrix1 = new int[rows][cols]; int[][] matrix2 = new int[rows][cols]; int[][] result = new int[rows][cols]; // Input for first matrix System.out.println("Enter elements for the first matrix:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix1[i][j] = sc.nextInt(); } } // Input for second matrix System.out.println("Enter elements for the second matrix:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix2[i][j] = sc.nextInt(); } } // Adding two matrices for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } // Display result System.out.println("Resultant Matrix after Addition:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } sc.close(); }}

Explanation

  1. Input Gathering: The program prompts the user to enter elements for two 3x3 matrices.
  2. Matrix Addition Logic: Each element in the result matrix is calculated by adding the corresponding elements from the two matrices.
  3. Displaying the Result: The resulting matrix is printed in a structured format.

Output Example

yaml
Enter elements for the first matrix:1 2 34 5 67 8 9Enter elements for the second matrix:9 8 76 5 43 2 1Resultant Matrix after Addition:10 10 10 10 10 10 10 10 10

Key Points to Remember

  • Both matrices must have the same number of rows and columns for the addition of two matrix in Java to work correctly.
  • The addition logic is straightforward, but proper indexing is crucial to avoid errors.

By following this guide, you can confidently implement the addition of two matrix in Java in your projects. This concept is widely used in data manipulation, graphics, and scientific computations.


manishmishra154

1 Blog posts

Comments