[Rcpp-devel] Rcpp:integer class?
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Dec 21 18:30:20 CET 2010
On Tue, Dec 21, 2010 at 12:23 PM, Cedric Ginestet
<c.ginestet05 at googlemail.com> wrote:
> Dear Rcpp experts,
>
> I am trying to find a way a single integer to a function. See the code
> below. I am here obliged to translage v1 as an IntegerVector and then to
> select only its first argument. Is there a better way of doing this, where I
> don't have to declare an entire vector for such a task?
>
> ###########################################
> src2 <- '
> IntegerVector x1(v1);
> IntegerMatrix xD(D);
> IntegerVector Drow(xD.ncol());
> for(int i=0; i<xD.ncol(); i++) Drow[i]=xD(x1[0],i);
> return Drow;
> '
> rowExtraction <-
> cxxfunction(signature(D="matrix",v1="integer"),body=src2,plugin="Rcpp",verbose=TRUE)
> rowExtraction(D,1)
> ############################################
>
Try this:
src2 <- '
int x1 = as<int>(v1);
IntegerMatrix xD(D);
IntegerVector Drow(xD.ncol());
for(int i=0; i<xD.ncol(); i++) Drow[i]=xD(x1,i);
return Drow;
'
rowExtraction <-
cxxfunction(signature(D="matrix",v1="integer"),body=src2,plugin="Rcpp",verbose=TRUE)
rowExtraction(matrix(1:25,5),1)
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the Rcpp-devel
mailing list