Open In App

Python List Equality | Program to check if two given matrices are identical

Improve
Improve
Like Article
Like
Save
Share
Report

We are given two square matrices of same order. Check if two given matrices are identical. 

Examples:

Input :     A = [ [1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3],
                  [4, 4, 4, 4]]
 
            B = [ [1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3],
                  [4, 4, 4, 4]]

Output:    Matrices are identical

We have existing solution for this problem please refer C Program to check if two given matrices are identical link. In python any iterable object is comparable so we can solve this problem quickly in python with the help of List Equality

Implementation:

Python3




# Function to check if two given matrices are identical
 
def identicalMatrices(A,B):
 
    if A==B:
        print ('Matrices are identical')
    else:
        print ('Matrices are not identical')
 
# Driver program
if __name__ == "__main__":
    A = [ [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]]
 
    B = [ [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]]
    identicalMatrices(A,B)


Output

Matrices are identical

Another approach to solving this problem without using loops is to use the built-in function all. This function returns True if all elements in an iterable are True, and False otherwise.

We can use this function to check if all elements in the matrices are equal by first zipping the two matrices together, then using a list comprehension to check if each element in the zipped list is equal. We can then pass the resulting list to the all function to check if all elements are equal.

Here is an example of this approach:

Python3




def identical_matrices(A, B):
    equal = all([a == b for a, b in zip(A, B)])
    if equal:
        print("Matrices are identical")
    else:
        print("Matrices are not identical")
 
A = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
B = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
identical_matrices(A, B)


Output

Matrices are identical

Approach#3: Using nested loops 

The approach checks if two matrices A and B are identical by comparing their dimensions and corresponding values element-wise using nested loops. If any element is found to be different, it returns False. Otherwise, it returns True.

Algorithm

1. Check if the dimensions of the two matrices are equal. If not, return False.
2. Using two nested loops, compare each element of both matrices. If any pair of corresponding elements are not equal, return False.
3. If all elements are equal, return True.

Python3




def are_matrices_identical(A, B):
    if len(A) != len(B) or len(A[0]) != len(B[0]):
        return 'Matrices are not identical'
    for i in range(len(A)):
        for j in range(len(A[0])):
            if A[i][j] != B[i][j]:
                return 'Matrices are not identical'
    return 'Matrices are identical'
A = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
B = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
print(are_matrices_identical(A, B))


Output

Matrices are identical

Time Complexity: O(mn) where m is the number of rows and n is the number of columns in the matrices.
Space Complexity: O(1)



Last Updated : 10 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads