subDist2Mat {dissimilarities}R Documentation

Dist2Mat subsetting

Description

Efficiently extracts a 2d submatrix of pairwise distances from a "dist" object.

Usage

subDist2Mat(dist, idx1, idx2)

Arguments

dist

A "dist" object, which can be computed via the stats::dist function, representing the full pairwise distance matrix between observations.

idx1

An integer vector, specifying the row indices of the subsetted matrix.

idx2

An integer vector, specifying the column indices of the subsetted matrix.

Details

This function efficiently subsets a "dist" object by row and column indices, returning the corresponding rectangular section as a numeric matrix. It avoids explicit conversion from the "dist" object to a dense "matrix", improving memory efficiency and computational speed, especially with large datasets.

Row names are retained. If it is null, as.character(idx1) and as.character(idx2) will be used as row and column names of the resulting matrix instead.

Value

A numeric matrix storing pairwise distances between observations column-indexed by idx1 and row-indexed by idx2.

Author(s)

Minh Long Nguyen edelweiss611428@gmail.com

Examples


library("microbenchmark")
x = matrix(rnorm(200), nrow = 50)
dx = dist(x)
#Randomly subsetting a 10x10 matrix
idx1 = sample(1:50, 10)
idx2 = sample(1:50, 10)
microbenchmark(base::as.matrix(dx)[idx1,idx2],
               proxy::as.matrix(dx)[idx1,idx2],
               subDist2Mat(dx, idx1, idx2))
#Check if equal
v1 = as.vector(base::as.matrix(dx)[idx1,idx2])
v2 = as.vector(subDist2Mat(dx, idx1, idx2))
all.equal(v1, v2)


[Package dissimilarities version 0.3.0 Index]