Skip to contents

The print_matrix() function prints a (possibly abbreviated) matrix:

print_matrix(x = 1, label = "single numeric")
#> single numeric : 1
print_matrix(x = LETTERS[1:26], label = "letters")
#> letters : character vector of length 26 
#> A B C ... Z
print_matrix(x = 1:3, coldots = 2)
#> double vector of length 3 
#> 1 ... 3
print_matrix(x = matrix(rnorm(99), ncol = 1), label = "single column matrix")
#> single column matrix : 99 x 1 matrix of doubles 
#>        [,1]
#> [1,]   -1.4
#> [2,]   0.26
#> [3,]  -2.44
#> ...     ...
#> [99,]  1.32
print_matrix(x = matrix(1:100, nrow = 1), label = "single row matrix")
#> single row matrix : 1 x 100 matrix of doubles 
#>      [,1] [,2] [,3] ... [,100]
#> [1,]    1    2    3 ...    100
print_matrix(x = matrix(LETTERS[1:24], ncol = 6), label = "big matrix")
#> big matrix : 4 x 6 matrix of characters 
#>      [,1] [,2] [,3] ... [,6]
#> [1,]    A    E    I ...    U
#> [2,]    B    F    J ...    V
#> [3,]    C    G    K ...    W
#> [4,]    D    H    L ...    X
print_matrix(x = diag(5), coldots = 2, rowdots = 2, simplify = TRUE)
#> [ 1 ... 0; ... ... ...; 0 ... 1 ]

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"

Add matrix column

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