+2 votes
in Programming Languages by (73.8k 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

+1 vote
by (348k points)
selected by
 
Best answer

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
>>>


...