Learn R Programming

kergp (version 0.5.7)

symIndices: Vector of Indices Useful for Symmetric or Anti-Symmetric Matrices.

Description

Vector of indices useful for symmetric or anti-symmetric matrices

Usage

symIndices(n, diag = FALSE)

Value

A list containing the following integer vectors, each with length \((n - 1) n / 2\).

i, j

Row and column indices for the lower triangle to be used in a two-index style.

kL

Indices for the lower triangle, to be used in single-index style. The elements are picked in column order. So if X is a square matrix with size n, then X[kL] is the vector containing the elements of the lower triangle of X taken in column order.

kU

Indices for the upper triangle, to be used in a single-index style. The elements are picked in row order. So if X is a square matrix with size n, then X[kU] is the vector containing the elements of the upper triangle of X taken in row order.

Arguments

n

Size of a square matrix.

diag

Logical. When FALSE the diagonal is omitted in the lower and upper triangles.

Details

This function is intended to provide computations which are faster than lower.tri and upper.tri.

Examples

Run this code
n <- rpois(1, lambda = 10)
L <- symIndices(n)
X <- matrix(1L:(n * n), nrow = n)
max(abs(X[lower.tri(X, diag = FALSE)] - L$kL))
max(abs(t(X)[lower.tri(X, diag = FALSE)] - L$kU))
cbind(row = L$i, col = L$j)

Run the code above in your browser using DataLab