Skip to contents

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.

Examples

# indices of diagonal elements
n <- 3
matrix(1:n^2, n, n)
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
matrix_diagonal_indices(n)
#> [1] 1 5 9

# 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
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    2    4    0
#> [3,]    3    5    6
matrix_diagonal_indices(n, triangular = "lower")
#> [1] 1 4 6

# 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
#>      [,1] [,2] [,3]
#> [1,]    1    2    4
#> [2,]    0    3    5
#> [3,]    0    0    6
matrix_diagonal_indices(n, triangular = "upper")
#> [1] 1 3 6