C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether two matrices are equalExplanationIn this program, we need to check whether given matrices are equal or not. Two matrices are said to be equal if and only if they satisfy the following conditions:
Consider the above example, where matrices A and B are equal as they have the same size and same corresponding elements. Algorithm
SolutionPython#Initialize matrix a a = [ [1, 2, 3], [8, 4, 6], [4, 5, 7] ]; #Initialize matrix b b = [ [1, 2, 3], [8, 4, 6], [4, 5, 7] ]; flag = True; #Calculates number of rows and columns present in first matrix row1 = len(a); col1 = len(a[0]); #Calculates number of rows and columns present in second matrix row2 = len(b); col2 = len(b[0]); #Checks if dimensions of both the matrices are equal if(row1 != row2 or col1 != col2): print("Matrices are not equal"); else: for i in range(0, row1): for j in range(0, col1): if(a[i][j] != b[i][j]): flag = False; break; if(flag): print("Matrices are equal"); else: print("Matrices are not equal"); Output: Matrices are equal C#include <stdio.h> #include <stdbool.h> int main() { int row1, col1, row2, col2; bool flag = true; //Initialize matrix a int a[][3] = { {1, 2, 3}, {8, 4, 6}, {4, 5, 7} }; //Initialize matrix b int b[][3] = { {1, 2, 3}, {8, 4, 6}, {4, 5, 7} }; //Calculates number of rows and columns present in first matrix row1 = (sizeof(a)/sizeof(a[0])); col1 = (sizeof(a)/sizeof(a[0][0]))/row1; //Calculates number of rows and columns present in second matrix row2 = (sizeof(b)/sizeof(b[0])); col2 = (sizeof(b)/sizeof(b[0][0]))/row2; //Checks if dimensions of both the matrices are equal if(row1 != row2 || col1 != col2){ printf("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) printf("Matrices are equal"); else printf("Matrices are not equal"); } return 0; } Output: Matrices are equal JAVApublic 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 C#using System; public class EqualMatrix { public static void Main() { 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.GetLength(0); col1 = a.GetLength(1); //Calculates the number of rows and columns present in the second matrix row2 = b.GetLength(0); col2 = b.GetLength(1); //Checks if dimensions of both the matrices are equal if(row1 != row2 || col1 != col2){ Console.WriteLine("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) Console.WriteLine("Matrices are equal"); else Console.WriteLine("Matrices are not equal"); } } } Output: Matrices are equal PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(1, 2, 3), array(8, 4, 6), array(4, 5, 7) ); //Initialize matrix b $b = array( array(1, 2, 3), array(8, 4, 6), array(4, 5, 7) ); $flag = true; //Calculates number of rows and columns present in first matrix $row1 = count($a); $col1 = count($a[0]); //Calculates number of rows and columns present in second matrix $row2 = count($b); $col2 = count($b[0]); //Checks if dimensions of both the matrices are equal if($row1 != $row2 || $col1 != $col2){ print("Matrices are not equal <br>"); } else { for($i = 0; $i < $row1; $i++){ for($j = 0; $j < $col1; $j++){ if($a[$i][$j] != $b[$i][$j]){ $flag = false; break; } } } if($flag) print("Matrices are equal <br>"); else print("Matrices are not equal <br>"); } ?> </body> </html> Output: Matrices are equal
Next Topic#
|