Computes the pairwise distance matrix between two sets of points


pairwise.dist(a, b, squared = TRUE, …)


a, b matrices (NxD) and (MxD), respectively, where each row represents a D-dimensional point.

squared return containing squared Euclidean distance.


Euclidean distance matrix (NxM). An attribute “squared” set to the value of param squared is provided.

See Also

res.dist(), dist2closest(), ball()


The continuous formation and rupture of non-covalent bonds (intra- and intermolecular), gives rise to dynamic molecular interactions, which are cosubstantial to life. These non-covalent interactions can adopt many forms, but all of them are fundamentally electrostatic in nature, and strongly influenced by distances.

Type of interactionEnergy dependence on the distance
Ion-induced dipole1/r^4
Dipole-indiced dipole1/r^5

Therefore, computing spatial distances (in ångströms, Å), either between atoms or residues, is a useful task in many different contexts. A number of function from the package ptm will help us in these tasks:

The most elementary of all these functions is, pairwise.dist(), which takes two sets of points (in the form of matrices) tailored by the user, and returns a distance matrix. Let’s just

a <- matrix(c(1,1,1,1,0,0,1,0,1), ncol = 3, byrow = TRUE)
rownames(a) <- c('point-1', 'point-2', 'point-3')
b <- matrix(c(0,0,0,1,1,1), ncol = 3, byrow = TRUE)
rownames(b) <- c('point-4', 'point-5')
##         [,1] [,2] [,3]
## point-1    1    1    1
## point-2    1    0    0
## point-3    1    0    1
##         [,1] [,2] [,3]
## point-4    0    0    0
## point-5    1    1    1

and now let’s compute the distances between the two sets of points:

pairwise.dist(a, b, square = FALSE)
##          point-4  point-5
## point-1 1.732051 0.000000
## point-2 1.000000 1.414214
## point-3 1.414214 1.000000
## attr(,"squared")
## [1] FALSE