<div dir="ltr"><div><div><div><div><div>Thanks for the reply and great libraries Conrad and Romain. <br><br>Conrad, my example is a bit too simple maybe, I am really doing:<br><br></div>for i from 1 to m:<br></div>   do computation<br>


</div>   store a row of results into a tall m x n matrix<br>
<br></div><div>...where i am pretty sure that the time required for computation is more than storing the results.<br></div><div><br></div>Out
 of curiosity, I tried to do some profiling for row filling of tall Armadillo matrices vs. column filling. Row filling becomes worse when the rows are long.  I guess the memory allocation could obscure some speed 
differences:<br>
<br>library(Rcpp)<br>library(inline)<br><br><div># fill a matrix by row with m0 rows and n0 columns<br></div>row.code <- '<br>   int m = Rcpp::as<int>(m0);<br>   int n = Rcpp::as<int>(n0);<br>   arma::mat mat = arma::zeros(m,n);<br>



   arma::rowvec vec = Rcpp::as<arma::rowvec>(vec0);<br>   for (int i = 0; i < m; i++) {<br>      mat.row(i) = vec;<br>   }<br>   return Rcpp::wrap(mat);<br>'<br></div>row.f <- cxxfunction(signature(m0="integer",n0="integer",vec0="numeric"),row.code,plugin="RcppArmadillo")<br>



<div><br># so here we swap, fill by column, m0 is the number of columns and n0 is the number of rows<br>col.code <- '<br>   int m = Rcpp::as<int>(m0);<br>   int n = Rcpp::as<int>(n0);<br>   arma::mat mat = arma::zeros(n,m);<br>



   arma::colvec vec = Rcpp::as<arma::colvec>(vec0);<br>   for (int i = 0; i < m; i++) {<br>      mat.col(i) = vec;<br>   }<br>   return Rcpp::wrap(mat);<br>'<br></div>col.f <- cxxfunction(signature(m0="<div>


integer",n0="integer",vec0="numeric"),col.code,plugin="RcppArmadillo")<br>
<br>> m <- 1e7<br>> n <- 10<br>> vec0 <- seq_len(n)<br>> system.time({row.f(m,n,vec0)})<br>   user  system elapsed <br>  0.779   1.022   1.801 <br>> system.time({col.f(m,n,vec0)})<br>   user  system elapsed <br>



  0.957   0.966   1.922 <br>> all.equal(row.f(m,n,vec0), t(col.f(m,n,vec0)))<br>[1] TRUE<br><br>> m <- 1e6<br>> n <- 100<br>> vec0 <- seq_len(n)<br>> system.time({row.f(m,n,vec0)})<br>   user  system elapsed <br>



  1.925   1.425   3.351 <br>> system.time({col.f(m,n,vec0)})<br>   user  system elapsed <br>  1.244   1.423   2.667 <br>> all.equal(row.f(m,n,vec0), t(col.f(m,n,vec0)))<br>[1] TRUE</div></div><div class="gmail_extra">

<br><br><div class="gmail_quote">On Wed, Mar 27, 2013 at 9:44 AM, Conrad S <span dir="ltr"><<a href="mailto:conradsand.arma@gmail.com" target="_blank">conradsand.arma@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

There's an even simpler way when using Armadillo, so that no loop is required:<br>
<a href="http://arma.sourceforge.net/docs.html#each_colrow" target="_blank">http://arma.sourceforge.net/docs.html#each_colrow</a><br>
<br>
For example:<br>
<br>
mat X(4,5);<br>
rowvec r(5);<br>
<br>
X.each_row() = r;<br>
<br>
<br>
(btw, in general it's more efficient to access matrices as columns<br>
rather than rows).<br>
<div class="im"><br>
<br>
On Wed, Mar 27, 2013 at 6:23 PM, Michael Love<br>
<<a href="mailto:michaelisaiahlove@gmail.com">michaelisaiahlove@gmail.com</a>> wrote:<br>
> As recommended, I have switched to using Armadillo matrices, which doesn't<br>
> print out these lines.<br>
</div>> ...<br>
<div class="HOEnZb"><div class="h5">> Example with Armadillo:<br>
><br>
> arma.code <- '<br>
>    arma::mat mat = Rcpp::as<arma::mat>(mat0);<br>
>    arma::rowvec vec = Rcpp::as<arma::rowvec>(vec0);<br>
>    for (int i = 0; i < mat.n_rows; i++) {<br>
>       mat.row(i) = vec;<br>
>    }<br>
>    return Rcpp::wrap(mat);<br>
> '<br>
> arma.f <-<br>
> cxxfunction(signature(mat0="numeric",vec0="numeric"),arma.code,plugin="RcppArmadillo")<br>
> arma.f(mat0,vec0)<br>
</div></div></blockquote></div><br></div>