Q:

Program to determine whether two matrices are equal

belongs to collection: Matrix Programs

0

Explanation

In 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:

  1. Both the matrices should have the same number of rows and columns.
  2. Both the matrices should have the same corresponding elements.

Consider the above example, where matrices A and B are equal as they have the same size and same corresponding elements.

Algorithm

  1. Declare and initialize two two-dimensional arrays a and b.
  2. Calculate the number of rows and columns present in the array a and store it in variables row1 and col1 respectively.
  3. Calculate the number of rows and columns present in array b and store it in variables row2 and col2 respectively.
  4. Initialize variable flag to true.
  5. Check if the size of both the arrays is equal. If arrays are not of equal size then, display the message " Matrices are not equal".
  6. If the size of both the arrays is equal then, loop through both the arrays and compare each element.
  7. If any of the corresponding elements are not equal then, set the flag to false and break the loop.
  8. If the flag is equal to true which implies matrices are equal. Else, matrices are not equal.

Input:

Matrix a = [1, 2, 3]  

           [8, 4, 6]  

           [4, 5, 7]  

  matrix b = [1, 2, 3]  

           [8, 4, 6]  

           [4, 5, 7]  

Output:

Matrices are equal

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#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

 

JAVA

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

 

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to display the lower triangular matrix... >>
<< Program to determine whether a given matrix is a s...