<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">Instead of posting this code to the list, a better idea is to write up your solution for RcppGallery. That way you can share code with a wider audience.</div><div class=""><br class=""></div><div class=""><a href="http://gallery.rcpp.org" class="">http://gallery.rcpp.org</a></div><div class=""><br class=""><div apple-content-edited="true" class="">
Dale Smith<br class=""><a href="mailto:dtsmith@mindspring.com" class="">dtsmith@mindspring.com</a><br class=""><br class="">

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Apr 3, 2015, at 8:02 AM, ogami musashi <<a href="mailto:uragami@hotmail.com" class="">uragami@hotmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Hello Romain and Christian,<br class=""><br class="">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.<br class=""><br class="">The whole thing took 54 sec in cpp versus 28 minutes in R! So thank you!<br class=""><br class="">When i have time i'll try to see if working on list of list is possible.<br class=""><br class="">Anyway so that it may be of use for anyone that would encounter the same problem here's my code:<br class=""><br class="">"#include <Rcpp.h><br class="">using namespace Rcpp;<br class=""><br class="">// [[Rcpp::export]]<br class=""><br class="">List RearrangeList(List x){<br class="">    List output;<br class="">    size_t ni=x.size();<br class=""><br class="">    NumericMatrix matout=x[0];<br class="">        int nrows=matout.nrow();<br class="">        int ncols=matout.ncol();<br class=""><br class=""><br class=""><br class=""><br class="">    //create matrixes in the output list<br class="">    for (int k=0; k<ncols; k++)<br class="">    {<br class="">        NumericMatrix mat_init_out_i(nrows,x.size());<br class="">        output.push_back(mat_init_out_i);<br class=""><br class="">    }<br class=""><br class="">    //Permutation (needs same number of columns for all matrices in the list)<br class="">    for (int j=0;j<ncols;j++)<br class="">    {<br class=""><br class=""><br class="">        NumericMatrix matx_bind(nrows,x.size());<br class=""><br class=""><br class=""><br class="">        for (int i=0;i<x.size();i++)<br class="">        {<br class="">            NumericMatrix matx=x[i];<br class="">            NumericVector vecx_i_j(matx.nrow());<br class="">            vecx_i_j=matx(_,j);<br class=""><br class="">            matx_bind(_,i)=vecx_i_j;<br class="">        }<br class="">        //output.push_back(matx_bind);<br class="">        output[j]=matx_bind;<br class="">    }<br class=""><br class=""><br class=""><br class="">return output;<br class="">}"<br class=""><br class="">On 30/03/15 11:05, ogami musashi wrote:<br class=""><blockquote type="cite" class="">Hello<br class=""><br class="">I'm a Rcpp newbie, so sorry if the question is trivial.<br class=""><br class="">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.<br class=""><br class="">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.<br class=""><br class="">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)<br class=""><br class=""><br class="">here are the objects in R:<br class=""><br class="">dwtlist: 1000 elements * 5 elements* (50000*16) matrix<br class="">sortdwtlist:16 elements*(50000*1000) matrix<br class=""><br class="">the code in R:<br class=""><br class="">for(i in 1:16){<br class=""><br class="">    for(j in 1:50000){<br class=""><br class="">        for (k in 1:1000){<br class=""><br class="">            sortdwtlist[[i]][j,k]<-dwtlist[[k]][[1]][j,i]<br class="">}<br class="">}<br class="">}<br class=""><br class=""><br class="">This takes about 27 minutes to complete.<br class=""><br class="">I thus tried to put in C++ using Rcpp (i'm using sourceCpp):<br class=""><br class="">#include <Rcpp.h><br class="">using namespace Rcpp;<br class=""><br class="">// [[Rcpp::export]]<br class=""><br class="">List rearrangelist(List x){<br class="">    int ni=15;<br class="">    int nj=49999;<br class="">    int nk=999;<br class="">    List output;<br class=""><br class="">for (int i= 0 ; i<ni ; i++){<br class="">    for (int j=0 ; j<nj ; j++) {<br class="">        for (int k=0 ; j<nk ; k++){<br class="">            output[i](j,k)=x[k][0](j,i);<br class="">        }<br class="">    }<br class="">}<br class="">return output;<br class="">}<br class=""><br class=""><br class="">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.<br class="">Can someone help me?<br class=""><br class="">Thank you!<br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">Rcpp-devel mailing list<br class=""><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel<br class=""></blockquote><br class="">_______________________________________________<br class="">Rcpp-devel mailing list<br class=""><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel<br class=""></div></blockquote></div><br class=""></div></body></html>