[Rcpp-devel] '[' operator of NumericMatrix

Peng Yu pengyu.ut at gmail.com
Thu Aug 23 00:11:29 CEST 2012


Hi,

I'm trying to use '[' operator to extract elements from NumericMatrix.
But it seems that it is not what I expected. Is it a bug or a feature?
I can use Column to extract columns and then extract elements in each
column, which is a tedious solution. Any better solution? Thanks!

BTW, where is '[' is documented for NumericMatrix?

> suppressPackageStartupMessages(library(inline))
> src='
+ Rcpp::NumericMatrix x(mx);
+ for(unsigned i = 0; i < x.nrow(); ++ i) {
+   for(unsigned j = 0; j < x.ncol(); ++ j) {
+     std::cout << i << " " << j << " " << x[i, j] << std::endl;
+   }
+ }
+ return Rcpp::wrap(0);
+ '
> fun=cxxfunction(signature(mx="numeric"), src,
+   plugin="Rcpp")
> orig=matrix(1:12, 3, 4)
> orig
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> fun(orig)
0 0 1
0 1 2
0 2 3
0 3 4
1 0 1
1 1 2
1 2 3
1 3 4
2 0 1
2 1 2
2 2 3
2 3 4
[1] 0
>


-- 
Regards,
Peng


More information about the Rcpp-devel mailing list