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

Romain François romain at r-enthusiasts.com
Mon Mar 30 13:09:27 CEST 2015


Hi, 

Perhaps you can use ListOf<NumericMatrix> instead of List, as output[i] does not know it is a matrix, so the operator()(int, int) don’t make sense. 

You’d need (untested, ont sure you can nest layers of ListOf) 

ListOf< NumericMatrix > x
ListOf< ListOf<NumericMatrix> > output ;


Otherwise, perhaps something like the more noisy code:

NumericMatrix output_i = output[i] ;
List x_k = x[k] ;
NumericMatrix x_k_0 = x_k[0] ;
output_i(j,k) = x_k_0(j,i) ;

Also, you need to actually creates the matrices in output. something like : 

List output(ni) ;
for (int i= 0 ; i<ni ; i++){
   output[i] = NumericMatrix( nj, nk ) ;
}

Romain

> Le 30 mars 2015 à 11:05, ogami musashi <uragami at hotmail.com> a écrit :
> 
> 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