r - How to sum 10 step rows of matrix in Rcpp? -


i want following results using rcpp. when big data, r slow. therefore, tried coding in rcpp.

x <- matrix(1:150, ncol = 5) z <- matrix(nrow = nrow(x) / 10, ncol = 5) (i in 1:5) {     (j in 1:(nrow(x) / 10)) {     k = (j - 1) * 10 + 1;     z[j, i] <- sum(x[k:(k+9), i])     } } x        [,1] [,2] [,3] [,4] [,5]  [1,]    1   31   61   91  121  [2,]    2   32   62   92  122  [3,]    3   33   63   93  123  [4,]    4   34   64   94  124  [5,]    5   35   65   95  125  [6,]    6   36   66   96  126  [7,]    7   37   67   97  127  [8,]    8   38   68   98  128  [9,]    9   39   69   99  129 [10,]   10   40   70  100  130 [11,]   11   41   71  101  131 [12,]   12   42   72  102  132 [13,]   13   43   73  103  133 [14,]   14   44   74  104  134 [15,]   15   45   75  105  135 [16,]   16   46   76  106  136 [17,]   17   47   77  107  137 [18,]   18   48   78  108  138 [19,]   19   49   79  109  139 [20,]   20   50   80  110  140 [21,]   21   51   81  111  141 [22,]   22   52   82  112  142 [23,]   23   53   83  113  143 [24,]   24   54   84  114  144 [25,]   25   55   85  115  145 [26,]   26   56   86  116  146 [27,]   27   57   87  117  147 [28,]   28   58   88  118  148 [29,]   29   59   89  119  149 [30,]   30   60   90  120  150  z       [,1] [,2] [,3] [,4] [,5]  [1,]   55  355  655  955 1255  [2,]  155  455  755 1055 1355  [3,]  255  555  855 1155 1455 

rcpp of code have tried follows.

#include <rcpp.h>  using namespace rcpp;  // [[rcpp::export]] numericvector mysum(numericmatrix x) {      int ncol = x.ncol();     int nrow = x.nrow();     int outrow = nrow / 10;     int i;     int j;     int k;     rcpp::numericmatrix z(outrow, ncol);      (i = 0; < ncol; i++) {         (j = 0; j < outrow; j++) {         k = j * 10;         rcpp::submatrix<realsxp> sm = x(range(k, k + 9), i);         rcpp::numericmatrix m(sm);         double s = rcpp::sum(m);         z(j, i) = s;         }     }   return z; } 

however, not move because of error. please tell me solution.

test.cpp: in function 'rcpp::numericvector mysum(rcpp::numericmatrix)': test.cpp:18:59: error: no match call '(rcpp::numericmatrix {aka rcpp::matrix<14>}) (rcpp::range, int&)' 

i prefer use rcpparmadillo when doing matrices, 1 reason because documentation (http://arma.sourceforge.net/docs.html#accu). rewrote code , seems work fine:

library(rcpparmadillo) library(rcpp)  cppfunction(" numericmatrix mysum(arma::mat x) {      int ncol = x.n_cols;     int nrow = x.n_rows;     int outrow = nrow / 10;     int i, j, k;     numericmatrix z(outrow, ncol);      (i = 0; < ncol; i++) {         (j = 0; j < outrow; j++) {             k = j * 10;             arma::mat sm = x(arma::span(k, k+9), i);             z(j, i) = arma::accu(sm);         }     }     return z; } ", depends = "rcpparmadillo")  x <- matrix(1:150, ncol = 5) mysum(x)      [,1] [,2] [,3] [,4] [,5] [1,]   55  355  655  955 1255 [2,]  155  455  755 1055 1355 [3,]  255  555  855 1155 1455 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -