0

Hello and sorry for the probably stupid question. I studied a bit of C++ in my past but in the last years I've only used R. I need to transform some R code for a package I wrote using Rcpp and in particular, I'm using RcppArmadillo. Now I checked the documentation but I spent the last hours trying to make this work. Let's say I have a matrix A and a vector of indices idx. If I want to extract a submatrix containing only the rows corresponding to the indices contained in idx, in R I'd do:

A[idx,]

while in Armadillo I'm doing this:

A.rows(idx);

where A is a mat object. However, I get the following error: not matching function for call to 'arma::Mat<double>::rows(arma::vec&)' What am I doing wrong? Thanks in advance for the help!

Andrew
  • 11

1 Answers1

2

Check out the Armadillo API documentation for submatrix in continuous and non-continuous states.

arma::mat X = arma::randu<arma::mat>(10, 10);

arma::uvec indices;
indices << 2 << 3 << 6 << 8;

// Subset
X.rows(indices);

For subsetting, please see the lengthy article that goes over such cases: https://gallery.rcpp.org/articles/armadillo-subsetting/

For even more guidance in translating, I wrote up a guide awhile back on common R operations and their equivalent in Armadillo.

https://thecoatlessprofessor.com/programming/cpp/common-operations-with-rcpparmadillo/

coatless
  • 20,011
  • 13
  • 69
  • 84