Python Program to check if two given matrices are identical



Matrix refers to a collection of numbers arranged in rows and columns to form a rectangular array. The numbers make up the matrix's entries, or elements.

We need to create a Python function to determine whether two given matrices are identical. In other words, we say that two matrices are identical if all of the elements in their respective places are the same.

Identical Matrices

Two matrices are only considered equal if and when they meet the requirements listed below −

  • There should be an equal number of rows and columns in each matrices.
  • The identical corresponding items should be present in both matrices.
$$\mathrm{Matrix\:A\begin{bmatrix}6 & 3 & 8\5 & 0& 4\7 & 1 & 4 \end{bmatrix}\:=\:Matrix\:B\begin{bmatrix}6 & 3 & 8 \5 & 0 & 2 \7 & 1 & 4 \end{bmatrix}}$$

The matrices A and B in the example above are equivalent because they have the same size and have the same corresponding elements.

Algorithm

Following is an algorithm to check if the two given matrices are identical −

  • Create the two two-dimensional arrays a and b, and then initialise them.
  • Determine the array's row and column counts, and save the results in the variables row1 and col1, accordingly.
  • Determine the array b's row and column counts, and save the results in the variables row2 and col2, accordingly.
  • Set variable flag's initial value to true.
  • Verify that the arrays' sizes are equivalent. Display "The two matrices are not equal" if the size of the arrays is not equal.
  • You should loop over both arrays and compare each entry if their sizes are equal.
  • Set the flag to false and end the loop if any of the relevant elements are not equal.

Example

Using loop

Following is an example to check if two matrices are equal or not −

#Initializing the matrix A A = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; #Initializing the matrix B B = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; flag = True; #Calculating the no of rows and columns present in the first matrix r1 = len(A); c1 = len(A[0]); #Calculating the no. of rows and columns present in the second matrix r2 = len(B); c2 = len(B[0]); #Checking whether the dimensions of both the matrices are equal if(r1 != r2 or c1 != c2): print("The two matrices are not equal"); else: for i in range(0, r1): for j in range(0, c1): if(A[i][j] != B[i][j]): flag = False; break; if(flag): print("The two matrices are equal"); else: print("The two matrices are not equal");

Output

Following is an output of the above code −

The two matrices are equal
The two matrices are equal
The two matrices are equal

Example

Compare the elements of each row in the two matrices. If they are same, go to the next row. If not, print "The two matrices are not Identical" and break the loop.The matrices are same if the loop did not break as shown in the below example −

#Initializing the matrix A A = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; #Initializing the matrix B B = [ [6, 3, 8], [5, 0, 2], [7, 1, 4] ]; X=0 for i in range(len(A)): for j in range(len(B)): if A[i][j]!=B[i][j]: X=1 break if X==1: break if X==0: print("The two matrices are Identical") else: print("The two matrices are not Identical")

Output

Following is an output of the above code −

The two matrices are Identical

Example

Make two matrices. After that, go over each element in both the first and second matrices and make a comparison between each one. Both matrices are identical if both are the same as shown in the following example −

V=4 # This function returns 1 if a[][] and b[][] are identical otherwise it returns 0 def identical(a,b): for i in range(s): for j in range(s): if (a[i][j] != b[i][j]): return 0 return 1 # the driver code a=[] s=int(input("Enter S for S x S matrix : ")) #using the list to store 2D array and getting the user input and storing it in the list print("Enter the elements ::>") for i in range(s): #temporarily list for storing the row row = [] for j in range(s): # adding the input to the row list row.append(int(input())) # adding the row to the list a.append(row) print(a) # [[6, 3, 8], [5, 0, 2], [7, 1, 4]] # Displaying the 2D array print("Displaying the array In the Matrix Form") for i in range(s): for j in range(s): print(a[i][j], end=" ") print() b=[] s=int(input("Enter S for S x S matrix : ")) #using the list to store 2D array and getting the user input and storing it in the list print("Enter the element ::>") for i in range(s): #temporarily list for storing the row row = [] for j in range(s): #add the input to row list row.append(int(input())) # adding the row to the list b.append(row) print(b) # [[6, 3, 8], [5, 0, 2], [7, 1, 4]] # Displaying the 2D array print("Display Array In Matrix Form") for i in range(s): for j in range(s): print(b[i][j], end=" ") print() if (identical(a, b)==1): print("The two matrices are Identical") else: print("The two matrices are not Identical")

Output

Following is an output of the above code −

Enter S for S x S matrix : 3
Enter the elements ::>
6
3
8
5
0
2
7
1
4
[[6, 3, 8], [5, 0, 2], [7, 1, 4]]
Displaying the array In the Matrix Form
6 3 8
5 0 2
7 1 4
Enter S for S x S matrix : 3
Enter the element ::>
6
3
8
5
0
2
7
1
4
[[6, 3, 8], [5, 0, 2], [7, 1, 4]]
Display Array In Matrix Form
17
6 3 8
5 0 2
7 1 4
The two matrices are Identical

Advertisements