Looking at your pseudo code it appears that you know how many columns are required in your matrix before run time. This situation lends itself well to generating the columns of your matrix as vectors as these can be manipulated with STL. Combining these vectors to a dataframe for return is very straight forward. Here is some code from Derk pulled from this list, here a, b, and c are Rcpp::NumericVector objects:
| > | >>> // create a new data frame | > | >>> Rcpp::DataFrame NDF = | > | >>> Rcpp::DataFrame::create(Rcpp::Named("a")=a, | > | >>> Rcpp::Named("b")=b, | > | >>> Rcpp::Named("c")=c); Since a dataframe is really a list of vectors, this should be quite efficient. A couple of months ago I went through a number of posts to this list with a subject something like "redimension a matrix". In that problem I had to use a matrix object in Rcpp, because the number of columns was only known at run time, based on characteristics identifiable in the arguments passed to my Rcpp function. At the time of my posts I was already convinced that I wanted to make an over estimate of the length of my matrix allocate it by creating an Rcpp::NumericMatrix and then expect to partially fill it by directly addressing the (row,col). This result was clearly faster than my use of STL's .push_back() for dynamic allocation, admittedly my simple mind could only deal with STL on Rcpp's SEXP objects. The problem that I then had was how to trim the resultant matrix to its smaller identified size after the filling loop was complete. Christian Gunning turned me on to using the RcppArmadillo package, which gives more flexibility working with a matrix. But in the end I found that the best result was to return my oversized matrix in a dataframe (this is done just as if it were a vector as in the above code) which I trimmed back in my R code. _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel