[Rcpp-devel] Returning Armadillo matrices with dimnames

Sameer D'Costa sameerdcosta at gmail.com
Thu May 2 17:16:18 CEST 2013


Hi,

I am trying to return several Armadillo matrices back to R and to set
the names for the rows and the columns of each matrix. I have tried
converting each Armadillo matrix to a NumericMatrix and then setting
the dimnames on that before returning a list to R. This seems to work
(see snippet below) however I am not sure if this is the right way to
go or if I am converting the matrices properly or if extra copies are
being made. Any advice will be appreciated.

library(RcppArmadillo)
library(inline)

a <- matrix(c(0.5,0.1,0.1,0.5),nrow=2)

code <- '
   arma::mat coeff = Rcpp::as<arma::mat>(a);
   coeff %= coeff; // some arma calculations

   Rcpp::CharacterVector names = Rcpp::CharacterVector::create("a", "b");

   Rcpp::NumericVector rcoeff(wrap(coeff)); // convert to Rcpp?
   rcoeff.attr("dimnames") = Rcpp::List::create(names, names);

   return Rcpp::List::create(Rcpp::Named("coeff") =  rcoeff);
 '

testfunc  <- cxxfunction(signature(a="numeric"), code,
                         plugin="RcppArmadillo")

testfunc(a)


Thanks in advance.

Regards
Sameer


More information about the Rcpp-devel mailing list