[Rcpp-devel] Can I fill a matrix row by row with std<vector> (or similar)

Dirk Eddelbuettel edd at debian.org
Fri Mar 1 19:05:53 CET 2013

On 1 March 2013 at 17:15, stat quant wrote:
| Hello list,
| I have several std::vector<double> in a STL container and would like to fill a
| Rcpp::NumericMatrix row by row with them, is there an easy way to do it, or

One (often mentioned) issue is that storage really is column-wise so you
could consider stacking by column, and the transpose at the end.

| should I iterate over all elements as in usual C++?
| I saw the `_` in this post on SO but I think it has a different purpose.
| cCode <- '
|     double v_value[] = {1.1,2.4};
|     vector<double> v(v_value, v_value + sizeof(v_value)/sizeof(double));
|     NumericMatrix m(3,2);
|     //Here is what I was hoping for
|     //for(int i=0; i<3; i++){
|     // m(i,_) = v;
|     //}

I believe that works or at least used to worked / is supposed to work.

That said, I also frequently take Armadillo for tasks such as this, it has
all the operations, and if I ever wanted the C++ routine in C++ standalone
I'd simply switch to Armadillo (rather than RcppArmadillo) headers.  Similar
strategies should work for RcppEigen if you prefer Eigen.


|     return m;
| ';
| R)
| fillMat <- cxxfunction(signature(), includes='using namespace std;', plugin=
| "Rcpp", body=cCode) 
| Regards
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  

More information about the Rcpp-devel mailing list