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.

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.

See also

Examples

x <- 1:10 d <- dist(x) # entries holding distances between the 3rd point and all the # other points computeDistIdx(length(x), 3)
#> [1] 2 10 18 19 20 21 22 23 24
# include an extra \code{NA} at the 3rd position to make the vector easily indexable i <- computeDistIdx(length(x), 3, includeMyself = TRUE) i
#> [1] 2 10 NA 18 19 20 21 22 23 24
# the distance between point 3 and 4: d[i[4]]
#> [1] 1
# entries holding distances between the 3rd point and points 5 and 7 computeDistIdx(length(x), 3, c(5, 7))
#> [1] 19 21