[Rcpp-devel] Slices of 3d arrays as matrices

Dirk Eddelbuettel edd at debian.org
Tue Oct 23 20:20:57 CEST 2012


Hi Giovanni,

On 23 October 2012 at 17:56, Giovanni Petris wrote:
| Hello,
| 
| I have a 3d array and I want to compute the eigendecomposition of each slice [,, k]. I am trying to make Rcpp see the relevant slice of the 3d array as a matrix. My experiments, along the lines illustrated below, have so far been unsuccessful. Does anybody have a suggestion? Thank you in advance!
| 
| 
|    NumericVector cpp_Sigma(Sigma); // 3d array, with dim(Sigma) = c(M,M,K), as a vector
|    NumericMatrix tmpMatrix;
|    List var_eigen;
|    Function eigen("eigen");
|    int k=2; // 3rd slice, for example
| 
|    tmpMatrix = cpp_Sigma[k * M * M]; 
|    tmpMatrix.attr("dim") = Dimension(M, M);
|    var_eigen = eigen(tmpMatrix);
|    return var_eigen;

I would recommend RcppArmadillo or RcppEigen for this.  

Rcpp itself has matrices but not that many operations on them.  RcppArmadillo
and RcppEigen will both give you access to a wide variety of decompositions
provided, respectively, by Armadillo and Eigen.  

There are a number of examples for either package in the list archives and on
other places. 

Dirk

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


More information about the Rcpp-devel mailing list