computeDistIdx: Indices into distance matrix.

computeDistIdxR Documentation

Indices into distance matrix.

Description

computeDistIdx computes the indices of entries in a distance matrix (as computed by dist) that hold the distances between a given point and one or more other points.

Usage

computeDistIdx(n, i, 
    j = setdiff(1:n, 
        i), includeMyself = FALSE)

Arguments

n

number of points

i

index of point to compute distance from

j

index of point(s) to compute distance to

includeMyself

if TRUE, and j equals setdiff(1:n, i), the returned vector includes an extra NA element corresponding to the non-existing index into the distance matrix holding the distance between point i and point i itself. This makes the length of the returned vector to be equal to n, such that the index corresponding to the distance between point i and point j resides at position j in the returned vector.

Value

a vector of indices of entries in the distance matrix related to the distances from the i-th point to the other points. The indices of entries corresponding to the distance between point j and point i are sorted in increasing order according to j. If includeMyself is TRUE, the returned vector includes an extra NA element corresponding to the non-existing index into the distance matrix holding the distance between point i and point i itself. This makes the length of the returned vector to be equal to n, such that the index corresponding to the distance between point i and point j resides at position j in the returned vector.

Author(s)

Tomas Sieger

See Also

computeIdxFromDistIdx

Examples

x <- 1:10
d <- dist(x)

# entries holding distances between the 3rd point and all the
# other points
computeDistIdx(length(x), 3)

# include an extra \code{NA} at the 3rd position to make the vector easily indexable
i <- computeDistIdx(length(x), 3, includeMyself = TRUE)
i
# the distance between point 3 and 4:
d[i[4]]

# entries holding distances between the 3rd point and points 5 and 7
computeDistIdx(length(x), 3, c(5, 7))


tsieger/tsiMisc documentation built on Oct. 10, 2023, 10:24 p.m.