Learn R Programming

flexclust (version 1.3-4)

dist2: Compute pairwise distances between two data sets

Description

This function computes and returns the distance matrix computed by using the specified distance measure to compute the pairwise distances between the rows of two data matrices.

Usage

dist2(x, y, method = "euclidean", p=2)

Arguments

x
A data matrix.
y
A vector or second data matrix.
method
the distance measure to be used. This must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski". Any unambiguous substring can be given.
p
The power of the Minkowski distance.

Details

This is a two-data-set equivalent of the standard function dist. It returns a matrix of all pairwise distances between rows in x and y. The current implementation is efficient only if y has not too many rows (the code is vectorized in x but not in y).

See Also

dist

Examples

Run this code
x = matrix(rnorm(20), ncol=4)
rownames(x) = paste("X", 1:nrow(x), sep=".")
y = matrix(rnorm(12), ncol=4)
rownames(y) = paste("Y", 1:nrow(y), sep=".")

dist2(x, y)
dist2(x, y, "man")

data(milk)
dist2(milk[1:5,], milk[4:6,])

Run the code above in your browser using DataLab