Learn R Programming

cccd (version 1.00.05)

pdist: Lp distances between vectors

Description

computes the Lp distance between vectors. When called with a single argument this is similar to dist. When called with two arguments, it returns a matrix containing the interpoint distances between the rows of the matrices.

Usage

pdist(x,y=NULL,d,p=2,w=NULL,arc=FALSE)
pdistcos(x,d=ncol(x),arc=FALSE)

Arguments

x
a vector or matrix.
y
a vector or matrix. If y is NULL, pdist returns dist(x). Otherwise it returns the distances between x and y.
d
the dimension of the data (if missing, it guesses it from x and y).
p
the p in Lp. p=Inf returns $L_\infty$. p=NA will call pdistcos.
w
optional weight vector for weighted distances.
arc
logical. Should the arc-length be returned instead of the cosine distance?

Value

  • a matrix of distances.

Details

This computes the $L_p$ distance between the points in x, or, if y is not NULL, between pairs of x and y. If p is Inf, the $L_{\infty}$ distance is computed. If w is given, it must be a vector of length equal to d, and is used to weight the contribution of the variates to the distance calculation. pdistcos returns the cosine dissimilarity, which is the normalized dot product of the vectors. If arc=TRUE, the arccos is returned, which is the arc-length distance. If p=NA, pdist will return pdistcos on the data.

See Also

dist

Examples

Run this code
x <- runif(10)

a <- pdist(x)

# a is a 10x10 matrix of distances

y <- runif(20)
a <- pdist(x,y)

# a is now a 10x20 matrix of distances (it infered that d=1)

z <- runif(10)
b <- pdist(x,z)

# b is a 1x1 matrix (it guessed that d=10)

b <- pdist(x,z,d=1)

# now b is a 10x10 matrix 

z <- matrix(runif(20),ncol=10)

b <- pdist(x,z)

# now b is 1x2

Run the code above in your browser using DataLab