Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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 and initialize them.
- Determine the arrays' row and column counts.
- Set a flag variable to track matrix equality.
- Verify that the arrays' sizes are equivalent.
- Loop through both arrays and compare each element if their sizes are equal.
- Set the flag to false if any corresponding elements are not equal.
Using Nested Loops with Flag Variable
This approach uses a flag variable to track whether matrices are identical ?
# Initializing matrix A
A = [
[6, 3, 8],
[5, 0, 2],
[7, 1, 4]
]
# Initializing matrix B
B = [
[6, 3, 8],
[5, 0, 2],
[7, 1, 4]
]
flag = True
# Calculating rows and columns in first matrix
r1 = len(A)
c1 = len(A[0])
# Calculating rows and columns in second matrix
r2 = len(B)
c2 = len(B[0])
# Checking whether dimensions are equal
if r1 != r2 or c1 != c2:
print("The two matrices are not equal")
else:
for i in range(r1):
for j in range(c1):
if A[i][j] != B[i][j]:
flag = False
break
if not flag:
break
if flag:
print("The two matrices are equal")
else:
print("The two matrices are not equal")
The two matrices are equal
Using Early Break Optimization
This method exits immediately when a mismatch is found ?
# Initializing matrix A
A = [
[6, 3, 8],
[5, 0, 2],
[7, 1, 4]
]
# Initializing matrix B (different from A)
B = [
[6, 3, 8],
[5, 9, 2], # Different element at position [1][1]
[7, 1, 4]
]
mismatch_found = False
for i in range(len(A)):
for j in range(len(A[0])):
if A[i][j] != B[i][j]:
mismatch_found = True
break
if mismatch_found:
break
if not mismatch_found:
print("The two matrices are identical")
else:
print("The two matrices are not identical")
The two matrices are not identical
Using a Function Approach
Creating a reusable function to check matrix equality ?
def are_matrices_identical(matrix1, matrix2):
"""Function to check if two matrices are identical"""
# Check dimensions first
if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
return False
# Check each element
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
if matrix1[i][j] != matrix2[i][j]:
return False
return True
# Test matrices
matrix_a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix_b = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix_c = [
[1, 2, 3],
[4, 5, 6]
]
# Test identical matrices
if are_matrices_identical(matrix_a, matrix_b):
print("Matrix A and Matrix B are identical")
else:
print("Matrix A and Matrix B are not identical")
# Test different dimension matrices
if are_matrices_identical(matrix_a, matrix_c):
print("Matrix A and Matrix C are identical")
else:
print("Matrix A and Matrix C are not identical")
Matrix A and Matrix B are identical Matrix A and Matrix C are not identical
Comparison of Methods
| Method | Advantages | Use Case |
|---|---|---|
| Flag Variable | Clear logic flow | Educational purposes |
| Early Break | Faster for large matrices | Performance-critical applications |
| Function Approach | Reusable and modular | Production code |
Conclusion
Checking matrix identity involves comparing dimensions first, then element-by-element comparison. The function approach provides the most reusable and clean solution for comparing matrices in Python programs.
