fastDist {dissimilarities} | R Documentation |
"dist" object computation
Description
Efficiently computes a "dist" object from a numeric matrix using various distance metrics.
Usage
fastDist(X, method = "euclidean", diag = FALSE, upper = FALSE, p = 2L)
Arguments
X |
A numeric matrix. |
method |
A character string specifying the distance metric to use. Supported methods include
|
diag |
A boolean value, indicating whether to display the diagonal entries. |
upper |
A boolean value, indicating whether to display the upper triangular entries. |
p |
A positive integer, required for computing Minkowski distance; by default p = 2 (i.e., Euclidean). |
Details
Calculates pairwise distances between rows of a numeric matrix and returns the result as a compact "dist" object, which stores the lower-triangular entries of a complete distance matrix. Supports multiple distance measures, including "euclidean", "manhattan", "maximum", "minkowski", "cosine", and "canberra". This implementation is optimised for speed, especially on large matrices.
Row names are retained. If it is null, as.character(1:nrow(X)) will be used as row names instead.
Value
A distance matrix of class "dist".
Author(s)
Minh Long Nguyen edelweiss611428@gmail.com
Examples
library("microbenchmark")
x = matrix(rnorm(200), nrow = 50)
microbenchmark(stats::dist(x, "minkowski", p = 5),
fastDist(x, "minkowski", p = 5))
v1 = as.vector(stats::dist(x, "minkowski", p = 5))
v2 = as.vector(fastDist(x, "minkowski", p = 5))
all.equal(v1, v2)