Discussion:
[Rcpp-devel] Creating a multidimensional array
Barth Riley
2018-07-31 18:55:44 UTC
Permalink
Dear list

I am trying to create a simple Rcpp function that creates a three-dimensional array based on the values of a four-dimensional array. The arrays are declared as NumericVectors with a dim attribute represent the number of levels of each dimension. Here is a sample of the code:

NumericVector sumby4(NumericVector X) {
Dimension dim = X.attr("dim");
if(dim.size() != 4) return NULL;

NumericVector ret = NumericVector::create(dim[0],dim[1],dim[2]);

for(int i = 0; i < dim[0]; i++) {
for(int j = 0; j < dim[1]; j++) {
for(int k = 0; k < dim[2]; k++) {
ret(i,j,k) = NA_REAL;



When I attempt to compile the package in which this function resides, I get the error message associated with the “ret(I,j,k) = NA_REAL” line:

No match for call to Rcpp::NumericVector Rcpp::Vector<14 Rcpp::PreserveStorage>})(int&, int&, int&)

I take from this message that the NumericVector ret cannot be indexed by three values, even though it has been dimensioned as a 3D array. What am I doing wrong here?

Barth
Dirk Eddelbuettel
2018-07-31 19:24:43 UTC
Permalink
On 31 July 2018 at 13:55, Barth Riley wrote:
| Dear list
|
| I am trying to create a simple Rcpp function that creates a three-dimensional array based on the values of a four-dimensional array. The arrays are declared as NumericVectors with a dim attribute represent the number of levels of each dimension. Here is a sample of the code:
|
| NumericVector sumby4(NumericVector X) {
| Dimension dim = X.attr("dim");
| if(dim.size() != 4) return NULL;
|
| NumericVector ret = NumericVector::create(dim[0],dim[1],dim[2]);
|
| for(int i = 0; i < dim[0]; i++) {
| for(int j = 0; j < dim[1]; j++) {
| for(int k = 0; k < dim[2]; k++) {
| ret(i,j,k) = NA_REAL;
| …
|
| When I attempt to compile the package in which this function resides, I get the error message associated with the “ret(I,j,k) = NA_REAL” line:
|
| No match for call to Rcpp::NumericVector Rcpp::Vector<14 Rcpp::PreserveStorage>})(int&, int&, int&)
|
| I take from this message that the NumericVector ret cannot be indexed by three values, even though it has been dimensioned as a 3D array. What am I doing wrong here?

Yes, the compiler barks because it sees no matching method.

Now, we _have_ examples for this somewhere I think. One thing that came up in a
quick search is

http://gallery.rcpp.org/articles/simple-array-class/

and you can see there that i) yes a multi-dim is used but ii) indexing games
need to be played. Maybe that helps.

Else arma::cube is a real 3d data I have used happily in the past. And these
days CRAN has xtensor (if you can do C++14).

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Continue reading on narkive:
Loading...