Skip to content

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

2 messages · stat quant, Dirk Eddelbuettel

#
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 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;
    //}

    return m;
';
R)
fillMat <- cxxfunction(signature(), includes='using namespace std;',
plugin="Rcpp", body=cCode)

Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130301/95f4cda9/attachment.html>
#
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.

Dirk

 ???????
| ??? 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