which_tri: Which in lower/upper triangle

Description Usage Arguments Value Examples

View source: R/which_lt.R

Description

Find where the original positions of components are in a matrix given a logical vector corresponding to the lower or upper triangle stored by columns. Similar to which(.., arr.ind = TRUE)

Usage

1
which_tri(cond, diag = FALSE, lower = TRUE)

Arguments

cond

logical vector of length that of the lower triangle

diag

logical: are the diagonal entries included?

lower

logical: is this the lower triangle? If FALSE it is the upper.

Value

a two column matrix with the row and column indices as the rows

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
set.seed(123)
X <- matrix(rnorm(20*2), 20, 2)
plot(X, asp = 1, pch = 16, las = 1, xlab = "x", ylab = "y")
dX <- dist(X)
ij <- which_tri(dX == max(dX))
points(X[as.vector(ij), ], col = "red", cex = 2, pch = 1)
segments(X[ij[1], 1], X[ij[1], 2],
         X[ij[2], 1], X[ij[2], 2], col = "red")
ij <- which_tri(dX == sort(dX, decreasing = TRUE)[2])
points(X[as.vector(ij), ], col = "blue", cex = 2, pch = 1)
segments(X[ij[1], 1], X[ij[1], 2],
         X[ij[2], 1], X[ij[2], 2], col = "blue")
polygon(X[chull(X), ], border = "sky blue")
rm(X, dX, ij)

MASSExtra documentation built on May 2, 2021, 9:07 a.m.