[Rcpp-devel] '[' operator of NumericMatrix
Dirk Eddelbuettel
edd at debian.org
Thu Aug 23 00:42:30 CEST 2012
On 22 August 2012 at 17:11, Peng Yu wrote:
| 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?
Neither but mostly your misunderstanding of C syntax carrying forward in C++
as never ever worked in a matrix class.
In C (and C++), the expression i,j leaved just j.
You want x(i, j) here.
Dirk
| 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
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list