【问题标题】:How to do pairwise calculation for two matrices with unequal dimensions in R如何对R中维度不等的两个矩阵进行成对计算
【发布时间】:2018-01-13 13:00:51
【问题描述】:

当矩阵 A 和矩阵 B 的维度不相等时,如何计算 R 中的欧几里得距离,如下所示:

我有两个矩阵,矩阵 A 和矩阵 B

矩阵 A:

     [,1][,2]
[1,]   1   1   
[2,]   1   2   
[3,]   2   1   
[4,]   2   2   
[5,]   10  1   
[6,]   10  2   
[7,]   11  1   
[8,]   11  2   
[9,]   5   5   
[10,]  5   6   

矩阵 B:

     [,1][,2][,3][,4][,5][,6]
[1,]   2   1   5   5  10   1
[2,]   1   1   2   1  10   1
[3,]   5   5   5   6  11   2
[4,]   2   2   5   5  10   1
[5,]   2   1   5   6  5    5
[6,]   2   2   5   5  11   1
[7,]   2   1   5   5  10   1
[8,]   1   1   5   6  11   1
[9,]   2   1   5   5  10   1
[10,]  5   6   11  1  10   2


I want the Result matrix for List 1 to store result of
the euclidean distance between row 1 to row 10 in matrix A and every two 
columns of row 1 in Matrix B as per below:

List [[1]]

        [1,]  [,2]  [,3]

    [1,] 1.00  5.66  9.00
    [2,] 0.00  1.00  9.00
    [3,] 5.66  6.40  10.05
    [4,]
    [5,]
    [7,]
    [8,]
    [9,]
    [10]

 For List 2, I want the Result matrix to store the result of the euclidean 
 distance between row 1 to row 10 in matrix A and every two columns of row 2 
 in Matrix B as per below: 

 List [[2]]

    [1,]  [,2]  [,3]

    [1,] 1.41  5.00  9.06
    [2,] 1.00  1.41  8.00
    [3,] 
    [4,]
    [5,]
    [7,]
    [8,]
    [9,]
    [10]

接下来,列表 3 用于矩阵 B 中的第 3 行

这应该一直持续到列表 10

例如,要在结果矩阵列表 1 中得到以下答案:

        [,1]
    [1,] 1.00

计算是:

    A(1,1) - From Matrix A
    B(2,1) - From Matrix B

    = sqrt((xA -xB)^2 + (yA -yB)^2)
    = sqrt((1-2)^2 + (1-1)^2)
    = 1.00

    xA and yA from Matrix A
    xB and yB from Matrix B

要获得以下问题的答案:

        [,2]
    [1,] 5.66

计算是:

    A(1,1) - From Matrix A
    B(5,5) - From Matrix B

    = sqrt((xA -xB)^2 + (yA -yB)^2)
    = sqrt((1-5)^2 + (1-5)^2)
    = 5.66

要获得以下问题的答案:

        [,3]
    [1,] 9.00

计算是:

    A(1,1) - From Matrix A
    B(10,1) - From Matrix B

    = sqrt((xA -xB)^2 + (yA -yB)^2)
    = sqrt((1-10)^2 + (1-1)^2)
    = 9.00

这是我目前拥有的,但它在矩阵 A 的第一行之间计算 矩阵 B 中的第 1 行,依此类推。我想要的是矩阵 A 中的每一行到列表 1 中矩阵 B 中的第一行,矩阵 A 中的每一行到矩阵 B 中的第二行,依此类推,直到矩阵 B 中的第 10 行;

    ObjCentDist <- function(matrixA, matrixB) {
       resultMatrix <- matrix(NA, nrow=dim(matrixA)[1],ncol=dim(matrixB[2]/2)
       for(i in 1:nrow(matrixA)) {
         for(j in 1:((dim(matrixB)[2])/2)) { 
           k = (j * 2) - 1
           resultMatrix[i,j] <- sqrt(rowSums((t(matrixA[i,])matrixB[i,k:k+1)])^2))  
         }
       }
       resultMatrix
    }



    matrixA <- matrix(c(1,1,1,2,2,1,2,2,10,1,10,2,11,1,11,2,5,5,5,6), ncol = 2, byrow = TRUE)
    matrixB <- matrix(c(2,1,5,5,10,1,1,1,2,1,10,1,5,5,5,6,11,2,2,2,5,5,10,1,2,1,5,6,5,5,2,2,5,5,11,1,2,1,5,5,10,1,1,1,5,6,11,1,2,1,5,5,10,1,5,6,11,1,10,2), nrow=10, ncol=6, byrow=TRUE)

我注意到了 pdist() 但我不确定如何在循环中使用它并在 mycase 中获得所需的输出,因为我对 R 非常陌生并且仍在学习

【问题讨论】:

  • 你能添加代码来生成你列出的两个矩阵吗?
  • 嗨,我已经编辑了问题以包含生成上述精确矩阵的代码

标签: r


【解决方案1】:

此解决方案需要 2 个不同的 apply 语句,因此从功能的角度来看,它可能不是最好的,但它应该可以提供您想要的结果。 (编辑以简化代码)

# Get Euclidean distance from A to all pairs in B
alldist <- function(A, B) {
  sapply(seq(2, length(B), by = 2), function(i) sqrt((A[,1] - B[i-1])^2 + (A[,2] - B[i])^2))
}

lapply(1:nrow(matrixB), function(n) alldist(A = matrixA, B = matrixB[n, ]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 2016-04-16
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多