I am trying to write a function in RcppArmadillo that dynamically appends rows to an array/matrix. It should work like rbind in R or pandas.concat in Python. (I am relying on C++ for efficiency.)
My specific objective is to take in a vector called foo and to produce a three-column matrix my_matrix, each row of which is determined by some condition. Because the condition needs to be checked for each triplet {i,j,k}, it involves a triple loop. This is what I have so far (words in BLOCK LETTERS are comments I include here):
/* (From my RcppArmadillo script) */
arma::mat myFunction(arma::vec foo) {
int n = foo.size();
// initialize first row of column names
arma::vec my_matrix[] = {"i", "j", "k"};
// loop and append rows
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
if (SOME CONDITION ABOUT i,j,k and foo) {
APPEND ROW {i,j,k} TO my_matrix
arma::vec new_row = {i,j,k};
my_matrix = join_vert(my_matrix, new_row);
}
}
}
}
return my_matrix;
}
I'm facing three issues:
- On the line
arma::vec new_row = {i,j,k};, I am told "non-constant-expression cannot be narrowed from type 'int' to 'double' in initializer list" - On the line
my_matrix = join_vert(my_matrix, new_row);, I am told "no matching function for call to 'join_vert'" - On the line
return my_matrix;, I am told "no viable conversion from 'arma::vec [3]' to 'arma::Mat' (aka 'Mat<<>>')"
Because I'm not familiar with C++ (especially with issues 2 and 3 that involve iterative modification) I am stuck. Could someone here help to troubleshoot? Thanks in advance!