0

How to calculate the euclidean distance in R between Matrix A and Matrix B when they have unequal dimensions 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 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] 

Next, List 3 is for row 3 in Matrix B

This should go on until List 10

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

 [,1] [1,] 1.00 

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:

 [,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:

 [,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 

This is what I have currently, but it calculates between row one in Matrix A with row 1 in Matrix B and so on. What i wanted is for every row in Matrix A to row one in Matrix B in List 1, for every row in Matrix A to row two in Matrix B and so on until row 10 in Matrix B;

 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) 

I noted the pdist() but I am not sure how to use it in loops and get the desired output in mycase as I am very new to R and still learning

2
  • Can you add code that produces the two matrices you listed? Commented Jan 13, 2018 at 13:11
  • Hi, I've edited the question to include the code to produce the exact matrices as above Commented Jan 13, 2018 at 13:27

1 Answer 1

2

This solution requires 2 different apply statements, so it's probably not the best from a functional perspective, but it should give the result you want. (edited to simplify code)

# 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, ])) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.