I have two matrices, A
and B
, with N_a
and N_b
rows, respectively. I need to calculate the Euclidean distance between all pairwise combinations of an element in A
(a
) and another in B
(b
), such that the output of the calculation is an Na by Nb matrix, where cell [a, b]
is the distance from a to b. I've started an example below.
# Example
set.seed(1)
A <- matrix(rnorm(1000, 5, 50), ncol = 5)
B <- matrix(rnorm(10000, 0, 50), ncol = 5)
# Return N_a x N_b matrix of euclidean distances, where [a,b] is the
# distance from a to b
A one-liner without loops, without additional packages, and a little bit faster:
euklDist <- sqrt(apply(array(apply(B,1,function(x){(x-t(A))^2}),c(ncol(A),nrow(A),nrow(B))),2:3,sum))
Speed comparison:
> microbenchmark(jogo = for (i in 1:nrow(A)) for (j in 1:nrow(B)) d[i,j] <- sqrt(sum((A[i,]-B[j,])^2)),
+ mra68 = sqrt(apply(array(app .... [TRUNCATED]
Unit: seconds
expr min lq mean median uq max neval
jogo 3.601533 4.724619 5.403420 5.549199 6.098734 6.470888 10
mra68 1.334661 1.635258 2.473297 2.542550 3.247981 3.348365 10