[Rcpp-devel] Turning a vector into a matrix

Dirk Eddelbuettel edd at debian.org
Fri May 31 13:24:20 CEST 2013


On 31 May 2013 at 10:28, Søren Højsgaard wrote:
| Romain,
| 
| Perhaps I now see the light at the end of the tunnel (hope it is not a train comming against me!): If I understand this correctly, the following
| 
| > template <int RTYPE>
| > Matrix<RTYPE> vec2matrix2(Vector<RTYPE> x, int nrow, int ncol) {
| >    return Matrix<RTYPE>(nrow, ncol, x.begin()); }
| 
| should do the dispatching properly if I call vec2matrix2 from a c++ function. Right? And all the other bells and whistles are merely to be able to call the function from R?

Yep.  From R we are bound by the   SEXP foo(SEXP a, SEXP b, ...)  interface
that .Call() can use.  

Hence Romain's 

    SEXP vec2matrix(SEXP x, int nrow, int ncol){

where the int values get converted by 'hidden' as<>() calls.   From C++,
however, you can call the above directly.

Hth, Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list