Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 time. The script I'm aiming for would look something like this:

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;
'
fun <- cxxfunction(signature(A = "numeric"), body = src, plugin="Rcpp")
fun(matrix(1,4,4))

The desired output would be this:

     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    2    2    2    2
[3,]    3    3    3    3
[4,]    4    4    4    4

The problem is obviously in this line, where I don't know how to refer to individual elements of the matrix.

Am[j,i] = Am[j,i] + Am[j-1,i];

Apologies if this is a stupid newbie question. Any hint would be appreciated!

like image 711
Vincent Avatar asked May 08 '11 23:05

Vincent


1 Answers

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> 
like image 106
Dirk Eddelbuettel Avatar answered Oct 04 '22 21:10

Dirk Eddelbuettel