[Rcpp-devel] Turning a vector into a matrix

Søren Højsgaard sorenh at math.aau.dk
Fri May 31 13:59:38 CEST 2013

Romain and Dirk:
Thanks for your help and patience!

-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org] 
Sent: 31. maj 2013 13:24
To: Søren Højsgaard
Cc: Romain Francois; rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Turning a vector into a matrix

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