Testing identity of two matrices

import numpy as np rows = 4 cols = 4 A = np.array([30,50,60,20,45,32,75,89,11,34,22,14,75,47,23,41]).reshape(rows, cols) B = np.array([30,50,60,20,45,32,75,89,11,34,22,14,75,47,23,41]).reshape(rows, cols) C = A ^ B print(C) ''' [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] No difference: The values in the two matrices are identical. ''' # Another way to check, without manually inspecting the matrix element-by-element. print(np.nonzero(C)) # The result must be an empty array. # (array([], dtype=int64), array([], dtype=int64)) # or: print(C[C != 0]) # [] # Someone changed 45 to 42 and 22 to 21 without telling us A = np.array([30,50,60,20,45,32,75,89,11,34,22,14,75,47,23,41]).reshape(rows, cols) B = np.array([30,50,60,20,42,32,75,89,11,34,21,14,75,47,23,41]).reshape(rows, cols) C = A ^ B print(C) ''' [[0 0 0 0] [7 0 0 0] [0 0 3 0] [0 0 0 0]] Difference: There are some non-zero values ''' print(C[C != 0]) # [7 3] # Find the exact rows and columns where the differences are diff_positions = [(i,j) for i in range(rows) for j in range(cols) if C[(i,j)] != 0] print("Row\tCol"); print("\n".join([str(i) + "\t" + str(j) for i, j in diff_positions])) ''' Row Col 1 0 2 2 ''' # Find the original and changed values original = [A[pos] for pos in diff_positions] changed = [B[pos] for pos in diff_positions] print("Original: " + str(original)) print("Changed: " + str(changed)) ''' Original: [45, 22] Changed: [42, 21] ''' # Fix changed with original values for i, pos in enumerate(diff_positions): B[pos] = A[pos] # Check C = A^B print(C) ''' [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] ''' print(C[C != 0]) # [] print(np.nonzero(A^B)) # (array([], dtype=int64), array([], dtype=int64))