Learning Objectives

Integration with R

Armadillo Vectors

Armadillo Matrices

Linear Algebra Operations

R to Armadillo Conversion Table

In the below table, A and B are a matrices and Q is a three-dimensional array (or “cube”). This table is based on the Matlab/Octave to Armadillo conversion table here.

R Armadillo Notes
A[1, 1] A(0, 0) indexing in Armadillo starts at 0
A[k, k] A(k-1, k-1)
nrow(A) A.n_rows read only
ncol(A) A.n_cols
dim(A)[[3]] Q.n_slices Q is a cube (3D array)
length(A) A.n_elem
A[, k] A.col(k) this is a conceptual example only; exact conversion from R to Armadillo syntax will require taking into account that indexing starts at 0
A[k, ] A.row(k)
A[, p:q] A.cols(p,q)
A[p:q, ] A.rows(p,q)
A[p:q, r:s] A(span(p,q),span(r,s)) A(span(first_row,last_row), span(first_col,last_col))
Q[, , k] Q.slice(k) Q is a cube (3D array)
Q[, , t:u] Q.slices(t,u)
Q[p:q, r:s, t:u] Q(span(p,q),span(r,s),span(t,u))
t(A) A.t()ortrans(A) matrix transpose
A[] <- 0 A.zeros() Fill a matrix with 0’s
A[] <- 1 A.ones() Fill a matrix with 1’s
A <- matrix(0, nrow = k, ncol = k) A = zeros<mat>(k,k) Initialize a 0 matrix
A <- matrix(1, nrow = k, ncol = k) A = ones<mat>(k,k) Initialize a 1 matrix
A * B A % B element-wise multiplication
A / B A / B element-wise division
solve(A, B) solve(A, B) Solve linear equation \(Ax = B\) for \(A\)
A <- A + 1; A++
A <- A - 1; A--
A <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = TRUE) A = {{1,2}, {3,4}} element initialization
X <- c(A) X = vectorise(A)
X = cbind(A, B) X = join_horiz(A, B)
X = rbind(A, B) X = join_vert(A, B)
A cout << A << endl or A.print("A=")

References

Farebrother, Richard William. 1988. Linear Least Squares Computations. Routledge. https://doi.org/10.1201/9780203748923.

National Science Foundation Logo American University Logo Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.