Skip to contents

Compute k-Nearest Neighbor Graph from Precomputed Distances

Usage

dist2knn(embedding, k, mode = "connectivity", ...)

Arguments

embedding

A distance matrix or `dist` object.

k

Integer. Number of nearest neighbors to compute.

mode

Character. Either `"dist"` or `"connectivity"` (default: `"connectivity"`).

...

Additional arguments passed to `knn_flat()`.

Value

A sparse matrix representing the k-NN graph (class `dgCMatrix`).

Details

Given a distance matrix, constructs a k-nearest neighbor graph as a sparse matrix. Optionally converts distances to Gaussian kernel similarities.

Examples

dmat <- dist(matrix(rnorm(100), ncol = 2))
knn_graph <- dist2knn(dmat, k = 5)
#> Error in 1:nrow(embedding): argument of length 0
print(knn_graph)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': object 'knn_graph' not found