简体   繁体   中英

How can I return a list of matrices from Rcpp to R?

I have a function in Rcpp that does something like this: it creates a list of matrices of type std::list, and intends to return that list of matrices back to R.

I attach here a reduced example:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
Rcpp::List splitImagesRcpp(arma::mat x) 
{
   std::list<arma::mat> listOfRelevantImages;
   int relevantSampleSize = x.n_rows;
   for(int k = 0; k < relevantSampleSize; ++k)
   {
      listOfRelevantImages.push_back(x.row(k));
   }
   return wrap(listOfRelevantImages);
}

The problem here is, I want to return to R a list of matrices, but I get a list of vectors . I have been trying a lot and looking at the documentation, but I can't seem to find a solution for this. It looks like wrap is doing its job but it is also wrapping my matrices recursively inside of the list.

I get something like this:

> str(testingMatrix)
List of 200
 $ : num [1:400] 1 1 1 1 1 1 1 1 1 1 ...
 $ : num [1:400] 1 1 1 1 1 1 1 1 1 1 ...

But I want to get something like this:

> str(testingMatrix)
List of 200
 $ : num [1:40, 1:10] 1 1 1 1 1 1 1 1 1 1 ...
 $ : num [1:40, 1:10] 1 1 1 1 1 1 1 1 1 1 ...

I want to do this from Rcpp, not in R. That is because I want to be able to interchange the function with a purely R programmed one, in order to measure the speedup.

Any help would be really appreciated!

Use the arma::field class that has the necessary plumbing to convert to and fro R and C++ .

Here's some sample code as to how one would work with the field class as your above example is not reproducible...

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::field<arma::mat> splitImagesRcpp(arma::mat x) {

    // Sample size
    int relevantSampleSize = x.n_rows;

    // Create a field class with a pre-set amount of elements
    arma::field<arma::mat> listOfRelevantImages(relevantSampleSize);

    for(int k = 0; k < relevantSampleSize; ++k)
    {
        listOfRelevantImages(k) = x.row(k);
    }


    return listOfRelevantImages;
}

Example:

set.seed(1572)
(x = matrix(runif(25), 5, 5))
#           [,1]        [,2]      [,3]      [,4]       [,5]
# [1,] 0.2984725 0.679958392 0.5636401 0.9681282 0.25082559
# [2,] 0.3657812 0.157172256 0.6101798 0.5743112 0.62983179
# [3,] 0.6079879 0.419813382 0.5165553 0.3922179 0.64542093
# [4,] 0.4080833 0.888144280 0.5891880 0.6170115 0.13076836
# [5,] 0.8992992 0.002045309 0.3876262 0.9850514 0.03276458
(y = splitImagesRcpp(x))
#      [,1]     
# [1,] Numeric,5
# [2,] Numeric,5
# [3,] Numeric,5
# [4,] Numeric,5
# [5,] Numeric,5
y[[1]]
#           [,1]      [,2]      [,3]      [,4]      [,5]
# [1,] 0.2984725 0.6799584 0.5636401 0.9681282 0.2508256

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM