Helpers for working with matrices

Get matrix indices

The matrix_indices() function returns matrix indices as character:

M <- diag(3)
matrix_indices(M)
#> [1] "11" "21" "31" "12" "22" "32" "13" "23" "33"
matrix_indices(M, "M_")
#> [1] "M_11" "M_21" "M_31" "M_12" "M_22" "M_32" "M_13" "M_23" "M_33"
matrix_indices(M, "M_", TRUE)
#> [1] "M_21" "M_31" "M_12" "M_32" "M_13" "M_23"

The insert_matrix_column() function can add a column to a matrix:

A <- diag(3)
x <- numeric(3)
insert_matrix_column(A, x, 0)
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    1    0    0
#> [2,]    0    0    1    0
#> [3,]    0    0    0    1
insert_matrix_column(A, x, 1)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    0    0
#> [2,]    0    0    1    0
#> [3,]    0    0    0    1
insert_matrix_column(A, x, 2)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    0    0
#> [2,]    0    1    0    0
#> [3,]    0    0    0    1
insert_matrix_column(A, x, 3)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    0    0
#> [2,]    0    1    0    0
#> [3,]    0    0    1    0