【问题标题】:How to compare two matrices to see if they are identical in R?如何比较两个矩阵以查看它们在 R 中是否相同?
【发布时间】:2014-04-12 15:31:15
【问题描述】:

例如,我有两个矩阵,我想知道它们在每个元素中是否相同。

mymatrix<-Matrix(rnorm(20),ncol=5)
mysvd<-svd(mymatrix) 
newmatrix<-mysvd$u %*% diag(mysvd$d) %*% t(mysvd$v)

我使用了以下方法来比较它们:

identical(Matrix(newmatrix), mymatrix)
all.equal(Matrix(newmatrix), mymatrix)

为什么第一个不返回 TRUE? 无论我使用矩阵包中的矩阵还是基础包中的矩阵

【问题讨论】:

    标签: r matrix


    【解决方案1】:

    由于差异非常小,它们并不完全相等(根据 identical):

    > max(abs(Matrix(newmatrix) - mymatrix))
    [1] 1.110223e-15
    

    但这些差异小于all.equal 内的默认tolerance

    > .Machine$double.eps ^ 0.5
    [1] 1.490116e-08
    

    所以identical 将返回FALSE 并且all.equal 将返回TRUE

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      > dput(Matrix(newmatrix))
      new("dgeMatrix"
          , x = c(-0.705193264426589, 0.68023073271425, 0.0726318059033283, -0.111055227906436, 
      -0.113940777963113, 0.726463241417717, -0.343435098646076, 0.885225942372688, 
      -0.549848405897803, -0.0227469387867766, -0.927524398860002, 
      0.58047674424687, 0.521144348439824, 0.279602090928527, -1.31686400403363, 
      0.906874499735628, -0.276997805548975, 0.632960950203858, 0.453881309098762, 
      -0.00528540521655077)
          , Dim = 4:5
          , Dimnames = list(NULL, NULL)
          , factors = list()
      )
      
      > dput(newmatrix)
      structure(c(-0.705193264426589, 0.68023073271425, 0.0726318059033283, 
      -0.111055227906436, -0.113940777963113, 0.726463241417717, -0.343435098646076, 
      0.885225942372688, -0.549848405897803, -0.0227469387867766, -0.927524398860002, 
      0.58047674424687, 0.521144348439824, 0.279602090928527, -1.31686400403363, 
      0.906874499735628, -0.276997805548975, 0.632960950203858, 0.453881309098762, 
      -0.00528540521655077), .Dim = 4:5)
      

      显然这些不是相同的数据结构。

      【讨论】:

        【解决方案3】:

        您要比较的第一个:

        > Matrix(newmatrix)
        4 x 5 Matrix of class "dgeMatrix"
                   [,1]       [,2]       [,3]        [,4]       [,5]
        [1,]  0.5052901 -0.3264201 -0.8576401 -0.62666359  2.1076090
        [2,]  0.2356111  0.4911067 -1.2376674  1.11231840  0.8576557
        [3,] -0.6244670  1.4423943 -1.2820541 -0.05297437 -2.0458810
        [4,] -0.2669079  1.1218459  0.6371571 -0.52168139  0.2163623
        

        与:

        > mymatrix
                   [,1]       [,2]       [,3]        [,4]       [,5]
        [1,]  0.5052901 -0.3264201 -0.8576401 -0.62666359  2.1076090
        [2,]  0.2356111  0.4911067 -1.2376674  1.11231840  0.8576557
        [3,] -0.6244670  1.4423943 -1.2820541 -0.05297437 -2.0458810
        [4,] -0.2669079  1.1218459  0.6371571 -0.52168139  0.2163623
        

        如果您阅读identical 的帮助,您会看到:The safe and reliable way to test two objects for being _exactly_ equal. 您正在将Matrix 类对象与 R 标准矩阵对象进行比较。它们非常不同。即使是具有不同属性的两个对象也不相同:

        > x
        a b c d 
        1 2 3 4 
        > y
        [1] 1 2 3 4
        > x==y
           a    b    c    d 
        TRUE TRUE TRUE TRUE 
        > identical(x,y)
        [1] FALSE
        

        【讨论】:

          【解决方案4】:

          您可以检查两个矩阵是否相同(相同),如下所示。

          假设您有 2 个矩阵,newMatrixoldMatrix,可以是任意维度。

          如果两个矩阵相同,which (which (newMatrix == oldMatrix) == FALSE) 将返回 integer (0)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多