Add Two Matrix Using Multi-dimensional Arrays

Add Two Matrix Using Multi-dimensional Arrays with Example

In this section, we will write a Java program that will add two matrices and display the output matrix, which will be the sum of the given matrices.

Example: Java Program to add two given Matrices

In the example below, we have two matrices, MatrixA and MatrixB, which we have declared as multi-dimensional arrays.

  • Two matrices can only be added or subtracted if their dimensions are the same, which means they have the same number of rows and columns. We have two MatrixA and MatrixB with identical rows and columns. These matrices will have the same rows and columns if they are added together.
  • This is how a matrix is declared as a multi-dimensional array:
    Matrix: This matrix is made up of two rows and four columns.

| 1 1 1 1 |
| 2 3 5 2 |

The declaration of this matrix as 2D array:

  • int[][] MatrixA = { {1, 1, 1, 1}, {2, 3, 5, 2} };

The for loop is used to add the corresponding elements of both matrices and store the addition values in the sum matrix. Sum[0][0] = MatrixA[0][0] + MatrixB[0][0], sum[0][1] = MatrixA[0][1] + MatrixB[0][1], and so on.

public class JavaExample {
    public static void main(String[] args) {
        int rows = 2, columns = 4;
        
        // Declaring the two matrices as multi-dimensional arrays
        int[][] MatrixA = { {1, 1, 1, 1}, {2, 3, 4, 3} };
        int[][] MatrixB = { {2, 3, 4, 5}, {2, 2, 4, -4} };
        
        /* Declaring a matrix sum, that will be the sum of MatrixA
         * and MatrixB, the sum matrix will have the same rows and
         * columns as the given matrices.
         */
        int[][] sum = new int[rows][columns];
        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                sum[i][j] = MatrixA[i][j] + MatrixB[i][j];
            }
        }
        // Displaying the sum matrix
        System.out.println("Sum of the given matrices is: ");
        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(sum[i][j] + "    ");
            }
            System.out.println();
        }
    }
}

Output:

Sum of the given matrices is:

  • 3 4 5 6
  • 4 5 8 -1

theudaipurstore.com

In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing. sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array convert a char array convert a char array convert a char array vv convert a char array vconvert a char array

Leave a Reply