+1 vote
in Programming Languages by (9k points)
I want to compare two big matrices to know if they have the same shape and elements. How can I check this?

1 Answer

0 votes
by (18k points)

If you use the array_equal() function of Numpy. It will return True if arrays/matrices have the same shape and elements, otherwise, False.

Here is an example:

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6]])
>>> b=np.array([[1,2,3],[4,5,6]])
>>> b1=np.array([[1,2,3],[2,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> b1
array([[1, 2, 3],
       [2, 5, 6]])

>>> np.array_equal(a,b)
True
>>> np.array_equal(a,b1)
False
>>>

...