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
}