
Euclidean distance between two vectors, or between column vectors of two matrices.
Source:R/L2_Distance.R
L2_distance.Rd
Quickly calculates and returns the Euclidean distances between m vectors in one set and n vectors in another. Each set of vectors is given as the columns of a matrix.
Arguments
- a
A d by m numeric matrix giving the first set of m vectors of dimension d as the columns of
a
.- b
A d by n numeric matrix giving the second set of n vectors of dimension d as the columns of
b
.- df
Indicator whether to force the diagonals of the returned matrix to be zero (
df = 1
) or not (the defaultdf = 0
).
Value
An m by n matrix containing the Euclidean distances between the column
vectors of the matrix a
and the column vectors of the matrix b
.
Details
This fully vectorized (VERY FAST!) function computes the Euclidean distance between two vectors by:
||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )
Originally written as L2_distance.m for Matlab by Roland Bunschoten of the University of Amsterdam, Netherlands.