如何计算两个矩阵之间的欧氏距离,每个矩阵的尺寸不相等

时间:2021-03-02 15:22:11

How to calculate the euclidean distance in R between Matrix A and Matrix B as per below:

如何计算矩阵A和矩阵B之间的欧氏距离如下:

I have two matrices that is Matrix A and Matrix B

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

Matrix 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   

Matrix 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 (euclidean distance) to be as per below:

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

    [1,] 1.00  5.66  9.00
    [2,] 1.00  1.41
    [3,]
    [4,]
    [5,]
    [7,]
    [8,]
    [9,]
    [10]

For every row in Matrix A, calculate the euclidean distance to every two column in each row Matrix B.

对于矩阵A中的每一行,计算每行中每两列的欧氏距离。

For example, to get the answer for the following in result matrix:

例如,要在结果矩阵中获得以下内容的答案:

        [,1]
    [1,] 

The calculation is:

计算如下:

    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

To get the answer for the following in result matrix:

要在结果矩阵中获得以下答案:

        [,2]
    [1,] 5.66

The calculation is:

计算如下:

    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

To get the answer for the following in result matrix:

要在结果矩阵中获得以下答案:

        [,3]
    [1,] 9.00

The calculation is:

计算如下:

    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

Currently, my codes below only works if Matrix A and B are of equal dimensions:

目前,我的代码仅适用于矩阵A和B的尺寸相同的情况:

    distance <- function(MatrixA, MatrixB) {
      resultMatrix <- matrix(NA, nrow=dim(MatrixA)[1], ncol=dim(MatrixB)[1])
      for(i in 1:nrow(MatrixB)) {
         resultMatrix[,i] <- sqrt(rowSums(t(t(MatrixA)-MatrixB[i,])^2))
      }
         resultMatrix
      }

1 个解决方案

#1


0  

You just need to change your for loop, so it calculates for each row all three columns of the result matrix:

您只需要更改for循环,因此它会为每一行计算结果矩阵的所有三列:

for(i in 1:nrow(matA)) 
{
  resultMatrix[i,1] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,1:2])^2))
  resultMatrix[i,2] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,3:4])^2))
  resultMatrix[i,3] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,5:6])^2))

}

Generalized for an arbitrary number of columns:

广义为任意数量的列:

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

#1


0  

You just need to change your for loop, so it calculates for each row all three columns of the result matrix:

您只需要更改for循环,因此它会为每一行计算结果矩阵的所有三列:

for(i in 1:nrow(matA)) 
{
  resultMatrix[i,1] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,1:2])^2))
  resultMatrix[i,2] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,3:4])^2))
  resultMatrix[i,3] <- sqrt(rowSums((t(MatrixA[i,])-MatrixB[i,5:6])^2))

}

Generalized for an arbitrary number of columns:

广义为任意数量的列:

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