简体   繁体   中英

How to calculate the euclidean distance in R between two matrices each with unequal dimensions

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

I have two matrices that is Matrix A and Matrix 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.

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:

    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
      }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM