Addition of Two Matrices in Java with Code

Matrix addition is a fundamental operation in linear algebra, where two matrices of the same dimensions are added together by adding their corresponding elements. In this article, we will explore how to implement matrix addition in Java with a clear code example.

What is Matrix?

A matrix is a two-dimensional array of numbers arranged in rows and columns. For example, a 2x3 matrix has two rows and three columns, or a 3x3 matrix has three rows and three columns.

Matrix addition rule

To add matrices, they must have the same dimensions; that is, both matrices must have the same number of rows and columns. To add a two-dimensional matrix, they must have two rows and two columns. Same as if we want to add three-dimensional matrices, both matrices must have three rows and three columns.

Code Example

Below is a simple Java program that demonstrates how to add two matrices:

import java.util.Scanner;

public class MatrixAddition {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Prompt user for the dimensions of the matrices
        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();
        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        // Declare the matrices
        int[][] matrixA = new int[rows][columns];
        int[][] matrixB = new int[rows][columns];
        int[][] sumMatrix = new int[rows][columns];

        // Input elements for the first matrix
        System.out.println("Enter elements of first matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print("Element [" + i + "][" + j + "]: ");
                matrixA[i][j] = scanner.nextInt();
            }
        }

        // Input elements for the second matrix
        System.out.println("Enter elements of second matrix:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print("Element [" + i + "][" + j + "]: ");
                matrixB[i][j] = scanner.nextInt();
            }
        }

        // Adding the two matrices
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                sumMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
            }
        }

        // Displaying the result
        System.out.println("Sum of two matrices:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(sumMatrix[i][j] + " ");
            }
            System.out.println(); // New line after each row
        }

        // Close the scanner
        scanner.close();
    }
}

Explanation of the code

  • Importing Scanner: The program begins by importing the Scanner class to read user input.
  • Matrix Declaration: We declare three matrices: matrixA, matrixB, and sumMatrix. The size of these matrices is determined by user input.
  • User Input: The program prompts the user to enter the number of rows and columns. It then uses nested loops to fill matrixA and matrixB with user-provided values.
  • Matrix Addition: Another set of nested loops is used to add corresponding elements from matrixA and matrixB, storing the results in sumMatrix.
  • Displaying Results: Finally, the program prints out the resulting sum matrix.

Two dimensions Matrix Addition

Add two dimensions Matrix

Three dimensions Matrix Addition

Two dimensions Matrix addition

Conclusion

In this article, we demonstrated how to perform matrix addition in Java using a simple console application. This example illustrates basic concepts such as arrays, loops, and user input handling in Java. Understanding how to manipulate matrices is essential for many applications in computer science, including graphics programming, machine learning, and scientific computing. Happy coding!


Similar Articles