Skip to content
Prev 311145 / 398506 Next

euclidean dist. between matrices

I may not understand correctly, but you have a matrix A with 15 rows
and 365 columns and a second matrix B with 1 row and 365 columns.
You say you want to compute the Euclidean distance between the first
column of A (15 numbers) against B. That won't work since the first
column of B is a single number, so I assume you want to compare each
row of A with the single row of B computing a total of 15 Euclidean
distances. If that is correct, there are a couple of possibilities:

sapply(1:15, function(x) sqrt(sum((B-A[x,])^2)))

Another approach would be to use function dist() and throw away the
distances you don't need:

head(dist(rbind(B, A)), 15)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
way
of
deeply