[Rcpp-devel] fill a NumericMatrix row by row

Conrad S conradsand.arma at gmail.com
Wed Mar 27 09:44:29 CET 2013


There's an even simpler way when using Armadillo, so that no loop is required:
http://arma.sourceforge.net/docs.html#each_colrow

For example:

mat X(4,5);
rowvec r(5);

X.each_row() = r;


(btw, in general it's more efficient to access matrices as columns
rather than rows).


On Wed, Mar 27, 2013 at 6:23 PM, Michael Love
<michaelisaiahlove at gmail.com> wrote:
> As recommended, I have switched to using Armadillo matrices, which doesn't
> print out these lines.
> ...
> Example with Armadillo:
>
> arma.code <- '
>    arma::mat mat = Rcpp::as<arma::mat>(mat0);
>    arma::rowvec vec = Rcpp::as<arma::rowvec>(vec0);
>    for (int i = 0; i < mat.n_rows; i++) {
>       mat.row(i) = vec;
>    }
>    return Rcpp::wrap(mat);
> '
> arma.f <-
> cxxfunction(signature(mat0="numeric",vec0="numeric"),arma.code,plugin="RcppArmadillo")
> arma.f(mat0,vec0)


More information about the Rcpp-devel mailing list