[Rcpp-devel] fill a NumericMatrix row by row
Michael Love
michaelisaiahlove at gmail.com
Wed Mar 27 09:23:58 CET 2013
Not a question here, but a note. Earlier there was an email about filling
a NumericMatrix row by row.
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-March/005390.html
I noticed that for Rcpp 0.10.2 it was possible to fill a NumericMatrix with
NumericVectors, but since Rcpp 0.10.3 it does so while printing out some
lines.
As recommended, I have switched to using Armadillo matrices, which doesn't
print out these lines.
-Mike
example with NumericMatrix, fill a matrix mat0 by row with the vector vec0:
library(Rcpp)
library(inline)
code <- '
Rcpp::NumericMatrix mat(mat0);
Rcpp::NumericVector vec(vec0);
for (int i = 0; i < mat.nrow(); i++) {
mat(i,_) = vec;
}
return Rcpp::wrap(mat);
'
f <-
cxxfunction(signature(mat0="numeric",vec0="numeric"),code,plugin="Rcpp")
mat0 <- matrix(c(1:4),nrow=2)
vec0 <- c(5,6)
f(mat0,vec0)
produces:
MatrixRow::get_parent_index(int = 0), parent_nrow = 2 >> 0
MatrixRow::get_parent_index(int = 1), parent_nrow = 2 >> 2
MatrixRow::get_parent_index(int = 0), parent_nrow = 2 >> 0
MatrixRow::get_parent_index(int = 1), parent_nrow = 2 >> 2
[,1] [,2]
[1,] 5 6
[2,] 5 6
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)
produces:
[,1] [,2]
[1,] 5 6
[2,] 5 6
> sessionInfo()
R Under development (unstable) (2013-03-20 r62335)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RcppArmadillo_0.3.800.1 inline_0.3.11 Rcpp_0.10.3
[4] Defaults_1.1-1 BiocInstaller_1.9.8
loaded via a namespace (and not attached):
[1] tools_3.1.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130327/158726bb/attachment.html>
More information about the Rcpp-devel
mailing list