Python比较两个矩阵

6
在下面显示的矩阵中,我想匹配两个矩阵中的第一个元素。如果第一个元素相等,则需要从两个矩阵中匹配第二个元素,以此类推。如果元素相同,则打印“same”,否则打印“not same”...
我的问题是如何以最优方式处理m*n的情况,其中m=n始终成立。
 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]  

1
a1a2的语法看起来不正确。 - Levon
我想以矩阵形式绘制它,因此我已按指示显示。 - Rajeev
6个回答

15

由于浮点数舍入误差,您可能会遇到一些问题。

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

建议使用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

11

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

所以 a == b 会递归地比较列表? - UselesssCat
1
当我这样做时,会出现 ValueError。 - Layne Bernardo

5
如果您正在使用numpy数组,请使用.all().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]

注意!当前答案是错误的!只要数组内的值都为真(没有false,零或空),x.all()将返回True。作者可能想要写成:(x == y).all() - Daniel Habenicht
1
@DanielHabenicht 根据建议进行了更正。 - coda

1
如果你想对矩阵进行操作,numpy 是你可以使用的最好的库。
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)

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

为什么使用print(W.todense().all()==W.todense().T.all()) #True返回True,而W不对称?

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0

以下是不使用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)])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接