Rcpp matrix: loop over rows, one column at a time

前端 未结 1 494
暗喜
暗喜 2021-01-30 18:25

This is my first time trying Rcpp and this very simple problem is giving me trouble. I want to use nested for loops to operate on individual values of a matrix, one column at a

相关标签:
1条回答
  • 2021-01-30 18:38

    You cannot use multiple indices in a single [ ] expression. It is a C language restriction that no C++ matrix class system or library I know of overcomes. So use ( ) instead.

    Fixing that and the mistake that you didn't actually pass src to cxxfunction(), we get this:

    R> src <- '
    +     Rcpp::NumericMatrix Am(A);
    +     int nrows = Am.nrow();
    +     int ncolumns = Am.ncol();
    +     for (int i = 0; i < ncolumns; i++) {
    +         for (int j = 1; j < nrows; j++) {
    +             Am(j,i) = Am(j,i) + Am(j-1,i);
    +         }
    +     }
    +     return Am;
    + '
    R> fun <- cxxfunction(signature(A = "numeric"), body = src, plugin="Rcpp")
    R> fun(matrix(1,4,4))
         [,1] [,2] [,3] [,4]
    [1,]    1    1    1    1
    [2,]    2    2    2    2
    [3,]    3    3    3    3
    [4,]    4    4    4    4
    R> 
    

    Lastly, note that Rcpp sugar has examples for working on entire row or column at a time, see the mailing list archives and vignette.

    Edit: Just to be explicit, here is the same using just one loop and the column-wise indexing of Rcpp sugar:

    R> src <- '
    +     Rcpp::NumericMatrix Am(A);
    +     int nrows = Am.nrow();
    +     for (int j = 1; j < nrows; j++) {
    +         Am(j,_) = Am(j,_) + Am(j-1,_);
    +     }
    +     return Am;
    + '
    R> fun <- cxxfunction(signature(A = "numeric"), body = src, plugin="Rcpp")
    R> fun(matrix(1,4,4))
         [,1] [,2] [,3] [,4]
    [1,]    1    1    1    1
    [2,]    2    2    2    2
    [3,]    3    3    3    3
    [4,]    4    4    4    4
    R> 
    
    0 讨论(0)
提交回复
热议问题