Java Program to Add Matrix

C++ multidimensional arrays

What is Matrix ?

In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. Matrices are often used to represent systems of linear equations, and they can be used to represent transformations in geometry and physics.

Operations on matrix :

Matrices can be added, subtracted, and multiplied by scalars (numbers). They can also be multiplied by other matrices, using a process called matrix multiplication. Matrix multiplication is different from the multiplication of two numbers, and it has its own set of rules.

To add two matrices in Java, you can use the following steps:

  1. Create a method that takes two matrices as input. Let’s call this method add.
  2. Check that the matrices have the same number of rows and columns. If they don’t, return an error message or throw an exception.
  3. Initialize a new matrix to hold the result of the addition. This matrix should have the same number of rows and columns as the input matrices.
  4. Use a loop to iterate over the elements of the matrices. For each element, add the corresponding elements from the two input matrices and store the result in the corresponding element of the result matrix.
  5. Return the result matrix.

Here is pseudo code that demonstrates how to add two matrices in Java:

public int[][] add(int[][] matrix1, int[][] matrix2) {
    // Check that the matrices have the same dimensions
    if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
        throw new IllegalArgumentException("Matrices must have the same dimensions.");
    }

    // Initialize a new matrix to hold the result
    int[][] result = new int[matrix1.length][matrix1[0].length];

    // Iterate over the elements of the matrices and add them
    for (int i = 0; i < matrix1.length; i++) {
        for (int j = 0; j < matrix1[0].length; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    // Return the result
    return result;
}

Example :

Run
public class Main {
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
        int[][] matrix2 = {{7, 8, 9}, {10, 11, 12}};

        int[][] result = add(matrix1, matrix2);

        // Print the result
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] add(int[][] matrix1, int[][] matrix2) {
        // Check that the matrices have the same dimensions
        if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
            throw new IllegalArgumentException("Matrices must have the same dimensions.");
        }

        // Initialize a new matrix to hold the result
        int[][] result = new int[matrix1.length][matrix1[0].length];

        // Iterate over the elements of the matrices and add them
        for (int i = 0; i < matrix1.length; i++) {
            for (int j = 0; j < matrix1[0].length; j++) {
                result[i][j] = matrix1[i][j] + matrix2[i][j];
            }
        }

        // Return the result
        return result;
    }
}

Output :

8 10 12 
14 16 18 

Example :

Run
public class Main {
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2}, {3, 4}};
        int[][] matrix2 = {{5, 6}, {7, 8}};
        int[][] matrix3 = {{9, 10}, {11, 12}};

        int[][] result = add(matrix1, matrix2, matrix3);

        // Print the result
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] add(int[][] matrix1, int[][] matrix2, int[][] matrix3) {
        // Check that the matrices have the same dimensions
        if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length ||
            matrix1.length != matrix3.length || matrix1[0].length != matrix3[0].length) {
            throw new IllegalArgumentException("Matrices must have the same dimensions.");
        }

        // Initialize a new matrix to hold the result
        int[][] result = new int[matrix1.length][matrix1[0].length];

        // Iterate over the elements of the matrices and add them
        for (int i = 0; i < matrix1.length; i++) {
            for (int j = 0; j < matrix1[0].length; j++) {
                result[i][j] = matrix1[i][j] + matrix2[i][j] + matrix3[i][j];
            }
        }

        // Return the result
        return result;
    }
}

Output :

15 18 
21 24 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription