matrix_diagonal_indices: Get indices of matrix diagonal

View source: R/matrix_diagonal_indices.R

matrix_diagonal_indicesR Documentation

Get indices of matrix diagonal

Description

This function returns the indices of the diagonal elements of a quadratic matrix.

Usage

matrix_diagonal_indices(n, triangular = NULL)

Arguments

n

[integer(1)]
The matrix dimension.

triangular

[NULL or character(1)]
If NULL (default), all elements of the matrix are considered. If "lower" ("upper"), only the lower- (upper-) triangular matrix is considered.

Value

An integer vector.

See Also

Other matrix helpers: check_correlation_matrix(), check_covariance_matrix(), check_transition_probability_matrix(), cov_to_chol(), diff_cov(), insert_matrix_column(), matrix_indices(), sample_correlation_matrix(), sample_covariance_matrix(), sample_transition_probability_matrix(), stationary_distribution()

Examples

# indices of diagonal elements
n <- 3
matrix(1:n^2, n, n)
matrix_diagonal_indices(n)

# indices of diagonal elements of lower-triangular matrix
L <- matrix(0, n, n)
L[lower.tri(L, diag=TRUE)] <- 1:((n * (n + 1)) / 2)
L
matrix_diagonal_indices(n, triangular = "lower")

# indices of diagonal elements of upper-triangular matrix
U <- matrix(0, n, n)
U[upper.tri(U, diag=TRUE)] <- 1:((n * (n + 1)) / 2)
U
matrix_diagonal_indices(n, triangular = "upper")

oeli documentation built on Oct. 16, 2024, 5:08 p.m.