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]
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With