fastDistAB {dissimilarities}R Documentation

Computing pairwise distances between rows of two matrices

Description

Efficiently computes pairwise distances between the rows of two numeric matrices using various distance metrics.

Usage

fastDistAB(A, B, method = "euclidean", p = 2L)

Arguments

A

A numeric matrix.

B

A numeric matrix.

method

A character string specifying the distance metric to use. Supported methods include "euclidean", "manhattan", "maximum", "minkowski", "cosine", and "canberra".

p

A positive integer, required for computing Minkowski distance; by default p = 2 (i.e., Euclidean).

Details

This function computes the full pairwise distance matrix between the rows of matrices A and B, without forming a concatenated matrix or performing unnecessary intermediate conversions. It supports multiple commonly used distance measures and is optimised for speed.

Row names in A and B are retained. If either rownames(A) or rownames(B) is null, as.character(1:nrow(A)) and as.character(1:nrow(B)) will be used as row and column names of the resulting matrix instead.

Value

A numeric matrix of dimensions nrow(A) by nrow(B), where each entry represents the distance between a row in A and a row in B.

Author(s)

Minh Long Nguyen edelweiss611428@gmail.com

Examples


library("microbenchmark")
X = matrix(rnorm(200), nrow = 50)
A = X[1:25,]
B = X[26:50,]
microbenchmark(proxy::dist(A,B, "minkowski", p = 5),
               fastDistAB(A,B, "minkowski", p = 5L))
#Check if equal
v1 = as.vector(proxy::dist(A,B, "minkowski", p = 5))
v2 = as.vector(fastDistAB(A,B, "minkowski", p = 5L))
all.equal(v1, v2)

[Package dissimilarities version 0.3.0 Index]