Armadillo's documentation page shows us how to initialize
a arma::mat
from a c array, here is how to do it
with a c++ std vector:
// [[Rcpp::export]] void testvec1() { std::vectory; y.push_back(1); y.push_back(2); y.push_back(3); // notice that &y.front() is the address of the first data element arma::mat x(&y.front(), 3, 1); x.print(); } // [[Rcpp::export]] void testvec2() { std::vector y; vector y1 {1, 2}; vector y2 {1, 3}; vector y3 {1, 4}; y.reserve(y1.size() * 3); y.insert(y.end(), y1.begin(), y1.end()); y.insert(y.end(), y2.begin(), y2.end()); y.insert(y.end(), y3.begin(), y3.end()); arma::mat x(&y.front(), 6, 1); x.print(); }
If you want to sourceCpp
this into R, you need
to enable c++11
features:
# R code Sys.setenv("PKG_CXXFLAGS"="-std=c++11") require(Rcpp)
0 comments:
Post a Comment