R/TriIndex.R
TriIndex.Rd
Given the number of rows in a symmetric matrix, calculate the row and column indices of the upper or lower triangles.
TriIndex(Nrow, which = "lower")
Nrow | The number of rows |
---|---|
which | Specify |
A two-column matrix.
A straightforward way to do this is to use
which(lower.tri(YourMatrix), arr.ind = TRUE)
, however, this can be
quite slow as the number of rows increases.
http://stackoverflow.com/a/20899060/1270695
Ananda Mahto
TriIndex(4)#> row col #> [1,] 2 1 #> [2,] 3 1 #> [3,] 4 1 #> [4,] 3 2 #> [5,] 4 2 #> [6,] 4 3TriIndex(4, "upper")#> row col #> [1,] 1 2 #> [2,] 1 3 #> [3,] 2 3 #> [4,] 1 4 #> [5,] 2 4 #> [6,] 3 4#> row col #> [1,] 2 1 #> [2,] 3 1 #> [3,] 4 1 #> [4,] 3 2 #> [5,] 4 2 #> [6,] 4 3