[Rcpp-devel] A Question about List of List of Vecotrs

Romain Francois romain at r-enthusiasts.com
Fri May 6 17:58:04 CEST 2011


Le 05/05/11 22:08, Fatemeh Riahi a écrit :
> Hi,
> I am trying to translate an R code to cpp,
> in R I have a structure like this:
> A=list();
> for(i in 1:n){
>    A[[i]]=list();
>    for(j in 1:m){
>     A[[i]][[j]]=rep(0, m);
>    }
> }
>
> I want to define this structre in Rcpp not to get it from R with a function
> I really appreciate if you help me
> Thanks
>
>
>
> --
> Fatemeh Riahi

Hi,

Your code pretty much litterally translates itself to C++.

require( Rcpp )
require( inline )

fx <- cxxfunction( signature( n_ = "integer", m_ = "integer" ), '

     int n = as<int>( n_ ), m = as<int>( m_ ) ;

     Rcpp::List out(n) ;
     for( int i=0; i<n; i++){
         Rcpp::List x(m) ;
         for( int j=0; j<m; j++){
             x[j] = rep( 0, m ) ;
         }
         out[i] = x ;
     }
     return out ;
', plugin = "Rcpp" )

fx( 5, 4 )

Hope this helps,

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
http://romain-francois.com
|- http://bit.ly/hdKhCy : Rcpp article in JSS
|- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011
`- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th




More information about the Rcpp-devel mailing list