Skip to content
Back to formatted view

Raw Message

Message-ID: <CAONgOD5EdAqm_i4jZzc4p8wU0HA6anLUz9XA0PWsAVbZ-1a3mA@mail.gmail.com>
Date: 2013-03-27T08:44:29Z
From: Conrad S
Subject: [Rcpp-devel] fill a NumericMatrix row by row
In-Reply-To: <CADqzidXqxsOmy-uXYTSPtzVT-dnF3RMnmT5kXHqQG_E47+cBeA@mail.gmail.com>

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)