Here we will write a java program to determine whether two matrices are equal or not. Two matrices are said to be equal if the dimensions (row and column count) of both the matrices are same and all the corresponding elements are equal. For example if two matrices m1 & m2 are equal then dimensions of m1 & m2 should be same and all the elements should match, m1[0][0] = m2[0][0], m1[0][1] = m2[0][1] and so on.
Program to determine whether two matrices are equal
Logic that we are following in the program below is:
if row and column count of m1 & m2 doesn’t match, no need to do further processing as we can simply say that matrices are not equal.
if row and column count matches then we are checking each element one by one and if any of the element doesn’t match we are storing the boolean value “false” in the boolean variable flag. Later we are displaying “Given matrices are not equal” if the flag value is false.
public class JavaExample { public static void main(String[] args) { int rowCount1, colCount1, rowCount2, colCount2; boolean flag = true; //Input matrix m1 int m1[][] = { {19, 8, 17}, {6, 14, 16}, {4, 15, 17} }; //Input matrix m2 int m2[][] = { {19, 8, 17}, {6, 14, 16}, {4, 15, 17} }; // Finding row and column count for matrix m1 rowCount1 = m1.length; colCount1 = m1[0].length; // Finding row and column count for matrix m2 rowCount2 = m2.length; colCount2 = m2[0].length; //Checking if size of both the matrices m1 & m2are equal if(rowCount1 != rowCount2 || colCount1 != colCount2){ System.out.println("Given matrices are not equal"); } else { // Here we are matching each element of m1 & m2 if // any element doesn't match we are setting the flag to false for(int i = 0; i < rowCount1; i++){ for(int j = 0; j < colCount1; j++){ if(m1[i][j] != m2[i][j]){ flag = false; break; } } } //If flag is not true it means the matrices are not equal if(flag) System.out.println("Given matrices are equal"); else System.out.println("Given matrices are not equal"); } } }
Output:
Given matrices are equal
Leave a Reply