python comparing two matrices

Question:

In the below shown matrices i want to match the first element in both the matrices. If the first element is equal then i need match the second element from both the matrices and so on..
if the elements are same then print “same” else print “not same”….

My question is how this in the optimal way also for m*n where m=n always

 for i in a1:
     for j in a2:
        if i!=j:
           break
         else:
           //compare the next corresponding columns and print "same" or "not same"


 a1=[1,44,55],[2,33,66],[3,77,91]  

 a2=[1,44,55],[2,45,66],[3,77,91]    

 OR 

 a1=[1,44,55]
    [2,33,66]
    [3,77,91]  

 a2=[1,44,55]
    [2,45,66]
    [3,77,91]  
Asked By: Rajeev

||

Answers:

What’s wrong with a1 == a2?

In [1]: a1=[[1,44,55],
   ...:     [2,33,66],
   ...:     [3,77,91]]

In [2]: a2=[[1,44,55],
   ...:     [2,45,66], # <- second element differs
   ...:     [3,77,91]]

In [3]: a1 == a2
Out[3]: False

In [4]: a1=[[1,44,55],
   ...:     [2,33,66],
   ...:     [3,77,91]]

In [5]: a2=[[1,44,55],
   ...:     [2,33,66],
   ...:     [3,77,91]]

In [6]: a1 == a2
Out[6]: True
Answered By: Benjamin Wohlwend

If you wanna do operations over matrix, numpy is the best library you could use

In [11]: a = numpy.matrix([[1,44,55],
    ...:                   [2,33,66],
    ...:                   [3,77,91]])

In [12]: b = numpy.matrix([[1,44,55],
    ...:                   [2,45,66],
    ...:                   [3,77,91]])

In [13]: a == b
Out[13]: 
matrix([[ True,  True,  True],
        [ True, False,  True],
        [ True,  True,  True]], dtype=bool)
Answered By: KurzedMetal

You may run into some issues due to floating point rounding errors.

>>> import numpy as np
>>> x = np.array(1.1)
>>> print x * x
1.21
>>> x * x == 1.21
False
>>> x * x
1.2100000000000002
>>> np.allclose(x * x, 1.21)
True

To compare whether two numerical matrices are equal, it is recommended that you use np.allclose()

>>> import numpy as np
>>> x = np.array([[1.1, 1.3], [1.3, 1.1]])
>>> y = np.array([[1.21, 1.69], [1.69, 1.21]])
>>> x * x
array([[ 1.21,  1.69],
       [ 1.69,  1.21]])
>>> x * x == y
array([[False, False],
       [False, False]], dtype=bool)
>>> np.allclose(x * x, y)
True
Answered By: Roy Hyunjin Han

In case you are using numpy arrays, use the .all() and .any().

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[1, 2, 3], [4, 5, 6]])
x.all() == y.all()
>> True

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[11, 21, 31], [41, 4, 61]])
(x == y).any()
>> True (since x[1][0] == y[1][1])
Answered By: coda

Below is solution with lists without using numpy.

def isIdentical(a: list, b: list) -> bool:
    rows, cols = len(a), len(a[0])
    return all([a[i][j] == b[i][j] for j in range(cols) for i in range(rows)])
Answered By: rajitbanerjee
from scipy import sparse
I = np.arange(0, 3).repeat(3)     
J =np.arange(9) 
V = np.arange(9) 
W = sparse.coo_matrix((V, (I, J)), shape=(9,9))
print(np.array_equiv(W.todense(),W.todense().T)) #False Shape consistent (broadcastable)
print(np.array_equal(W.todense(),W.todense().T)) #False (exact shape)
print(W.todense().all()==W.todense().T.all()) #True
print(W.todense==W.todense().T)
print(W.todense()[:,1], W.todense()[1,:])

Why using print(W.todense().all()==W.todense().T.all()) #True returns True, while W is not symmetric?

Answered By: Fatima Ansarizadeh
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.