Hi Ramon,<br><br>Option (b) would be more efficient, at the expense of slightly more code.<br><br>However, you can use option (a) to quickly get something working. Once you test that your code works okay, you can always optimise it by selectively refactoring it into (b).<br>
<br>If the code in question is not inside a critical loop, you may not need to bother with the refactoring. Eg. if most of the time is spent inside matrix multiplication, or solve(), or svd(), etc, refactoring would make little overall difference. <br>
<br><br><br>On Monday, December 10, 2012, Ramon Diaz-Uriarte <<a href="mailto:rdiaz02@gmail.com">rdiaz02@gmail.com</a>> wrote:<br>><br>> Dear All,<br>><br>> I am using RcppArmadillo, and I am creating matrices in the C++ code<br>
> (i.e., these are not matrices passed from R). The sizes of these matrices<br>> might need to increase dynamically during run time, with elements being<br>> added at the end. What is the recommended way of doing this:<br>
><br>><br>> a) Just insert elements as needed. E.g.: Y.insert_cols(Y.n_cols, the_new_thing);<br>><br>> b) Create initial object. Double size of object (using resize) each time I<br>> run out of space. Copy the_new_thing to the appropriate place.<br>
><br>><br>><br>> I thought b) was a reasonable manual approach (e.g., Skiena, "The<br>> algorithm design manual, 2nd ed", p. 67), but I think I've read that for<br>> std::vector, a) (with push_back), is a better choice than b) (using<br>
> reserve, not resize).<br>><br>><br>> For me, a) involves writing less code.<br>><br>> Thanks,<br>><br>><br>> R.<br>><br>><br>> --<br>> Ramon Diaz-Uriarte<br>> Department of Biochemistry, Lab B-25<br>
> Facultad de Medicina<br>> Universidad Autónoma de Madrid<br>> Arzobispo Morcillo, 4<br>> 28029 Madrid<br>> Spain<br>><br>> Phone: +34-91-497-2412<br>><br>> Email: <a href="mailto:rdiaz02@gmail.com">rdiaz02@gmail.com</a><br>
> <a href="mailto:ramon.diaz@iib.uam.es">ramon.diaz@iib.uam.es</a><br>><br>> <a href="http://ligarto.org/rdiaz">http://ligarto.org/rdiaz</a><br>><br>> _______________________________________________<br>
> Rcpp-devel mailing list<br>> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a>