Skip to content

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

3 messages · Fatemeh Riahi, Dirk Eddelbuettel, Romain Francois

#
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
#
Thank you for posting here. But please be patient -- the list is read by
many, but not everybody can reply within minutes.  Also, once posted here, no
point in crossposting to stackoverflow.com.
On 5 May 2011 at 17:08, Fatemeh Riahi wrote:
| 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

There are lots of example of how to create a list using Rcpp.  I suggest you
study those, in the documentation, other packages, list archives, ....  

Create a simple list, fill it with three elements.  Then create a second
list, fill it with an element or two as well as the first list.  Then work on
the rep part.  

Feel free to come back with concrete examples, preferably using inline (see
the list archives for numerous examples).  We are glad to help, especially if
we can see that you made an honest effort.  

But we unfortunately lack the time to write your code for you.

Hope this 
Dirk
#
Le 05/05/11 22:08, Fatemeh Riahi a ?crit :
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