Description Usage Arguments Details Value Note References See Also Examples

Computes the Euclidean distance between rows of two matrices.

1 2 3 |

`X` |
matrix of some size |

`Y` |
matrix of some size |

Computes Euclidean distance between two vectors A and B as:

`||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )`

and vectorizes to rows of two matrices (or vectors).

`pdist2`

is an alias for `distmat`

, while `pdist(X)`

is the
same as `distmat(X, X)`

.

matrix of size `m x n`

if `x`

is of size `m x k`

and
`y`

is of size `n x k`

.

If `a`

is `m x r`

and `b`

is `n x r`

then

`apply(outer(a,t(b),"-"),c(1,4),function(x)sqrt(sum(diag(x*x))))`

is the `m x n`

matrix of distances between the `m`

rows
of `a`

and `n`

rows of `b`

.

This can be modified as necessary, if one wants to apply distances other than the euclidean.

BUT: The code shown here is 10-100 times faster, utilizing the similarity between Euclidean distance and matrix operations.

Copyright (c) 1999 Roland Bunschoten for a Matlab version on MatlabCentral
under the name `distance.m`

. Translated to R by Hans W Borchers.

1 2 3 4 5 6 7 8 |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.