To determine whether two matrices are equal, certain conditions need to be met:
- The matrices must have the same number of rows and columns.
- The corresponding elements in both matrices must be the same.
Algorithm to Check If Two Matrices are Equal
-
- STEP 1: START
- STEP 2: DEFINE row1, col1, row2, col2
- STEP 3: INITIALIZE first matrix a[][] ={{1, 2, 3}, {8, 4,6}, {4, 5,7}}
- STEP 4: INITIALIZE second matrix b[][] ={{1 2, 3}, {8, 4, 6}{4, 5, 7}}
- STEP 5: row1 = a.length
- STEP 6: col1 = a[0].length
- STEP 7: row2 =b.length
- STEP 8: col2 = b[0].length
- STEP 9: if(row1!=row2 || col1!=col2)
then
PRINT “No”
else
go to step 10; - STEP 10: REPEAT STEP 11 UNTIL i<row1
//for(i=0; i<row1; i++) - STEP 11: REPEAT STEP12 UNTIL j<col1
//for(j=0; j<col1; j++) - STEP 12: if(a[i][j]=b[i][j]) then
flag =false
break - STEP 13: if(flag)
then PRINT “Yes”
else
PRINT “No” - STEP 14: END
Java Code to Check if Two Matrices are Equal
public class EqualMatrix { public static void main(String[] args) { int row1, col1, row2, col2; boolean flag = true; //Initialize matrix a int a[][] = { {1, 2, 3}, {8, 4, 6}, {4, 5, 7} }; //Initialize matrix b int b[][] = { {1, 2, 3}, {8, 4, 6}, {4, 5, 7} }; //Calculates the number of rows and columns present in the first matrix row1 = a.length; col1 = a[0].length; //Calculates the number of rows and columns present in the second matrix row2 = b.length; col2 = b[0].length; //Checks if dimensions of both the matrices are equal if(row1 != row2 || col1 != col2){ System.out.println("Matrices are not equal"); } else { for(int i = 0; i < row1; i++){ for(int j = 0; j < col1; j++){ if(a[i][j] != b[i][j]){ flag = false; break; } } } if(flag) System.out.println("Matrices are equal"); else System.out.println("Matrices are not equal"); } } }
Output:
Matrices are equal
In conclusion, the provided Java code successfully checks if two matrices are equal by comparing the corresponding elements of both matrices, ensuring that each element is identical. Follow tutorials.freshersnow.com to learn more.