[Rcpp-devel] Accessing list of list members to permute them

ogami musashi uragami at hotmail.com
Fri Apr 3 14:02:56 CEST 2015


Hello Romain and Christian,

Following christian advice on learning how to walk before running but 
taking romain's hints i succeeded in writing a code that does the same 
permutation but on a list of matrices. I had in fact always access the 
same nested list element which is a matrix i simply created a new 1000 
elements list with matrices inside.

The whole thing took 54 sec in cpp versus 28 minutes in R! So thank you!

When i have time i'll try to see if working on list of list is possible.

Anyway so that it may be of use for anyone that would encounter the same 
problem here's my code:

"#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]

List RearrangeList(List x){
     List output;
     size_t ni=x.size();

     NumericMatrix matout=x[0];
         int nrows=matout.nrow();
         int ncols=matout.ncol();




     //create matrixes in the output list
     for (int k=0; k<ncols; k++)
     {
         NumericMatrix mat_init_out_i(nrows,x.size());
         output.push_back(mat_init_out_i);

     }

     //Permutation (needs same number of columns for all matrices in the 
list)
     for (int j=0;j<ncols;j++)
     {


         NumericMatrix matx_bind(nrows,x.size());



         for (int i=0;i<x.size();i++)
         {
             NumericMatrix matx=x[i];
             NumericVector vecx_i_j(matx.nrow());
             vecx_i_j=matx(_,j);

             matx_bind(_,i)=vecx_i_j;
         }
         //output.push_back(matx_bind);
         output[j]=matx_bind;
     }



return output;
}"

On 30/03/15 11:05, ogami musashi wrote:
> Hello
>
> I'm a Rcpp newbie, so sorry if the question is trivial.
>
> I have an R object which is a list of 1000 elements. Each elements is 
> a result from a discrete wavelet transform (from package wmtsa) with 5 
> elements. One of them have a 50000 rows and 16 colums.
>
> The first level (1000) are in fact random signals of 50 000 days. On 
> each of them i ran a DWT that separated each signal into 16 components 
> (of decreasing frequency) for each day thus resulting in a 50000 days 
> * 16 components matrix for each of the 1000 signals.
>
> I would like to sort it like that: for each component (a list of 16 
> elements) having for each day (50000 rows) the 1000 signals (1000 
> columns)
>
>
> here are the objects in R:
>
> dwtlist: 1000 elements * 5 elements* (50000*16) matrix
> sortdwtlist:16 elements*(50000*1000) matrix
>
> the code in R:
>
> for(i in 1:16){
>
>     for(j in 1:50000){
>
>         for (k in 1:1000){
>
>             sortdwtlist[[i]][j,k]<-dwtlist[[k]][[1]][j,i]
> }
> }
> }
>
>
> This takes about 27 minutes to complete.
>
> I thus tried to put in C++ using Rcpp (i'm using sourceCpp):
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
>
> List rearrangelist(List x){
>     int ni=15;
>     int nj=49999;
>     int nk=999;
>     List output;
>
> for (int i= 0 ; i<ni ; i++){
>     for (int j=0 ; j<nj ; j++) {
>         for (int k=0 ; j<nk ; k++){
>             output[i](j,k)=x[k][0](j,i);
>         }
>     }
> }
> return output;
> }
>
>
> But it doesn't work. error stops at the line of permutation, so i'm 
> sure there's something with the types inside the list.
> Can someone help me?
>
> Thank you!
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list