forked from rcc-uchicago/R-large-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscale.cpp
More file actions
32 lines (26 loc) · 766 Bytes
/
scale.cpp
File metadata and controls
32 lines (26 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Rcpp.h>
using namespace Rcpp;
// The same as scale(X,center = TRUE,scale = TRUE), except that the
// input matrix X is modified directly. The return value can be
// ignored; it is always zero.
//
// Input argument "a" must be the vector of column means, and input
// "b" must be the vector of column standard deviations.
//
// [[Rcpp::export]]
double scale_rcpp (NumericMatrix& X, NumericVector& a, NumericVector& b) {
// Get the number of rows and columns of the matrix.
int nr = X.nrow();
int nc = X.ncol();
int i, j;
double aj, bj;
// Repeat for each column.
for(j = 0; j < nc; j++) {
aj = a(j);
bj = b(j);
// Repeat for each row.
for(i = 0; i < nr; i++)
X(i,j) = (X(i,j) - aj) / bj;
}
return 0;
}