[R] Rcpp/RcppArmadillo - matrix manipulation

Karin Lebl leblkarin at hotmail.com
Mon Jun 17 16:38:49 CEST 2013


Hello all,
 
I’ve been working with R for some years, but I am beginner to Rcpp (and have no experience with C++).
 
I am trying to set zeros in a matrix to Inf. In R I would do this:

x=matrix(8:0,3) 
x[which(x==0)]=Inf
 

My try with Rcpp and RcppArmadillo looked like this (as I do not know how it works with inf I just set it to 100 in a first step):
 
library(Rcpp)
library(inline)
library(RcppArmadillo)
 
code <- '
 arma::mat M0 = Rcpp::as<arma::mat>(a);
 arma::mat M1 = Rcpp::as<arma::mat>(a);
 arma::uvec xx = find (M1==0); 
M1(xx)=100;
 M1=M0+M1;
 return Rcpp::wrap(M1);
 '
tf <- cxxfunction(signature(a="numeric"), code,plugin="RcppArmadillo")
tf(x) 

But this just produces errors *… However, setting the index “manually” would work (changing M1(xx)=100 to M1(8)=100).
 
What do I have to change to make it work?
 
If I understood it correctly, it should be possible to use inf (http://arma.sourceforge.net/docs.html&constants). Can I do this simply by setting 
M1(xx)=inf; ?
 
Thanks to anyone who takes the time to answer my questions.
 
Best wishes,
Karin
 

* They are long and (I think) not very informative. But I can provide them if requested. 		 	   		  


More information about the R-help mailing list