Function File: pdist2 (x, y)
Function File: pdist2 (x, y, metric)

Compute pairwise distance between two sets of vectors.

Let X be an MxP matrix representing m points in P-dimensional space and Y be an NxP matrix representing another set of points in the same space. This function computes the M-by-N distance matrix D where D(i,j) is the distance between X(i,:) and Y(j,:).

The optional argument metric can be used to select different distances:

"euclidean" (default)
"sqeuclidean"

Compute the squared euclidean distance, i.e., the euclidean distance before computing square root. This is ideal when the interest is on the order of the euclidean distances rather than the actual distance value because it performs significantly faster while preserving the order.

"chisq'"

The chi-squared distance between two vectors is defined as: d(x, y) = sum ((xi-yi)^2 / (xi+yi)) / 2. The chi-squared distance is useful when comparing histograms.

"cosine"

Distance is defined as the cosine of the angle between two vectors.

"emd"

Earth Mover’s Distance (EMD) between positive vectors (histograms). Note for 1D, with all histograms having equal weight, there is a simple closed form for the calculation of the EMD. The EMD between histograms x and y is given by sum (abs (cdf (x) - cdf (y))), where cdf is the cumulative distribution function (computed simply by cumsum).

"L1"

The L1 distance between two vectors is defined as: sum (abs (x-y))

See also: pdist.

Package: statistics