6

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]  
2
  • 1
    The syntax for a1, a2 doesn't look correct Commented Jun 1, 2012 at 13:32
  • I wanted to draw that in a matrix form so i had shown as indicated Commented Jun 1, 2012 at 13:33

6 Answers 6

16

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
Sign up to request clarification or add additional context in comments.

Comments

11

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

2 Comments

So a == b compares list recursively?
I get a ValueError when I do this
6

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

Comments

1

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)

Comments

1

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

Comments

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.