Learn R Programming

MASSExtra (version 1.2.2)

which_tri: Which in lower/upper triangle

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

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

Value

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

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.

Examples

Run this code
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)

Run the code above in your browser using DataLab