pairdist.default
Pairwise distances
Computes the matrix of distances between all pairs of points in a set of points
Usage
## S3 method for class 'default':
pairdist(X, Y=NULL, \dots, period=NULL, method="C", squared=FALSE)
Arguments
- X,Y
- Arguments specifying the coordinates of a set of points.
Typically
X
andY
would be numeric vectors of equal length. AlternativelyY
may be omitted andX
may be a list with two components - ...
- Ignored.
- period
- Optional. Dimensions for periodic edge correction.
- method
- String specifying which method of calculation to use.
Values are
"C"
and"interpreted"
. Usually not specified. - squared
- Logical. If
squared=TRUE
, the squared distances are returned instead (this computation is faster).
Details
Given the coordinates of a set of points,
this function computes the Euclidean distances between all pairs of
points, and returns the matrix of distances.
It is a method for the generic function pairdist
.
The arguments X
and Y
must determine
the coordinates of a set of points. Typically X
and
Y
would be numeric vectors of equal length. Alternatively
Y
may be omitted and X
may be a list with two components
named x
and y
, or a matrix or data frame with two columns.
Alternatively if period
is given,
then the distances will be computed in the `periodic'
sense (also known as `torus' distance).
The points will be treated as if they are in a rectangle
of width period[1]
and height period[2]
.
Opposite edges of the rectangle are regarded as equivalent.
If squared=TRUE
then the squared Euclidean distances
$d^2$ are returned, instead of the Euclidean distances $d$.
The squared distances are faster to calculate, and are sufficient for
many purposes (such as finding the nearest neighbour of a point).
The argument method
is not normally used. It is
retained only for checking the validity of the software.
If method = "interpreted"
then the distances are
computed using interpreted R code only. If method="C"
(the default) then C code is used. The C code is somewhat faster.
Value
- A square matrix whose
[i,j]
entry is the distance between the points numberedi
andj
.
See Also
Examples
x <- runif(100)
y <- runif(100)
d <- pairdist(x, y)
d <- pairdist(cbind(x,y))
d <- pairdist(x, y, period=c(1,1))
d <- pairdist(x, y, squared=TRUE)