This function inserts a column into a matrix.
Arguments
- A
[
matrix()
]
Amatrix
.- x
[
atomic()
]
The column to be added, of lengthnrow(A)
.Can also be a single value.
- p
[
integer())
]
The position(s) where to add the column, one or more of:p = 0
appends the column leftp = ncol(A)
appends the column rightp = n
inserts the column between then
-th and(n + 1)
-th column ofA
.
See also
Other matrix helpers:
check_correlation_matrix()
,
check_covariance_matrix()
,
check_transition_probability_matrix()
,
cov_to_chol()
,
diff_cov()
,
matrix_diagonal_indices()
,
matrix_indices()
,
sample_correlation_matrix()
,
sample_covariance_matrix()
,
sample_transition_probability_matrix()
,
stationary_distribution()
Examples
A <- diag(3)
x <- 1:3
insert_matrix_column(A, x, 0)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 0 0
#> [2,] 2 0 1 0
#> [3,] 3 0 0 1
insert_matrix_column(A, x, 1)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1 0 0
#> [2,] 0 2 1 0
#> [3,] 0 3 0 1
insert_matrix_column(A, x, 2)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 0 1 0
#> [2,] 0 1 2 0
#> [3,] 0 0 3 1
insert_matrix_column(A, x, 3)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 0 0 1
#> [2,] 0 1 0 2
#> [3,] 0 0 1 3
### also single value
x <- 2
insert_matrix_column(A, x, 0)
#> [,1] [,2] [,3] [,4]
#> [1,] 2 1 0 0
#> [2,] 2 0 1 0
#> [3,] 2 0 0 1
### also multiple positions
insert_matrix_column(A, x, 0:3)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 2 1 2 0 2 0 2
#> [2,] 2 0 2 1 2 0 2
#> [3,] 2 0 2 0 2 1 2
### also trivial case
insert_matrix_column(matrix(nrow = 0, ncol = 0), integer(), integer())
#> <0 x 0 matrix>