[Rcpp-devel] fill a NumericMatrix row by row

Romain Francois romain at r-enthusiasts.com
Wed Mar 27 10:37:58 CET 2013


I removed it in the revision 4294. Sorry for the inconvenience.

Romain

Le 27/03/13 09:23, Michael Love a écrit :
> 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
>
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/ZTFLDo : Simpler R help tooltips
`- http://bit.ly/YFsziW : R Help tooltips



More information about the Rcpp-devel mailing list