subCols {dissimilarities} | R Documentation |
Subsetting a "dist" object by columns
Description
Efficiently extracts a column-wise subset of a "dist" object, returning the corresponding submatrix of pairwise distances. # nolint
Usage
subCols(dist, idx)
Arguments
dist |
A "dist" object, which can be computed via the stats::dist function, representing pairwise distances between observations. |
idx |
An integer vector, specifying the column indices of the subsetted matrix. |
Details
This function extracts specified columns from a "dist" object without explicit conversion to a dense distance "matrix", resulting in better performance and reduced memory overhead. Particularly useful when only a subset of distances is needed for downstream tasks.
Row names are retained. If it is null, as.character(1:nObs) and as.character(idx) will be used as row and column names of the resulting matrix instead.
Value
A numeric "matrix" containing the pairwise distances between all rows and the specified columns.
Author(s)
Minh Long Nguyen edelweiss611428@gmail.com
Examples
library("microbenchmark")
x = matrix(rnorm(200), nrow = 50)
dx = dist(x)
#Randomly subsetting a 50x10 matrix
idx = sample(1:50, 10)
microbenchmark(base::as.matrix(dx)[1:50,idx],
proxy::as.matrix(dx)[1:50,idx],
subCols(dx, idx))
#Check if equal
v1 = as.vector(base::as.matrix(dx)[1:50,idx])
v2 = as.vector(subCols(dx, idx))
all.equal(v1, v2)