forked from jkrijthe/RSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
74 lines (58 loc) · 1.42 KB
/
utils.cpp
File metadata and controls
74 lines (58 loc) · 1.42 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat rowMax(const arma::mat& X) {
arma::mat A = arma::zeros<arma::mat>(X.n_rows,1);
for (unsigned int i=0; i<X.n_rows; i++) {
// Rcout << X.row(i).max() << std::endl;
A(i) = X.row(i).max();
}
return(A);
}
// [[Rcpp::export]]
arma::colvec rowMax2(const arma::mat& X) {
arma::colvec A = max(X,1);
return(A);
}
// [[Rcpp::export]]
arma::mat which_rowMax(const arma::mat& X) {
arma::mat A = arma::zeros<arma::mat>(X.n_rows,1);
arma::uword row;
arma::uword col;
for (unsigned int i=0; i<X.n_rows; i++) {
X.row(i).max(row,col);
A(i) = col+1;
}
return(A);
}
// [[Rcpp::export]]
arma::mat which_rowMax2(const arma::mat& X) {
arma::ucolvec I = index_max(X,1);
arma::mat A = arma::zeros<arma::mat>(X.n_rows,1);
for (unsigned int i=0; i<X.n_rows; i++) {
A(i) = I(i)+1;
}
return(A);
}
// [[Rcpp::export]]
arma::mat sort_matrix(const arma::mat& X) {
arma::mat A = sort(X,"descend",1);
return(A);
}
// [[Rcpp::export]]
arma::mat rowwise_addition(arma::mat A, arma::rowvec x)
{
A.each_row() += x;
return A;
}
// [[Rcpp::export]]
arma::mat factor_to_dummy_cpp(Rcpp::IntegerVector y, int c) {
Rcpp::IntegerVector xy(y);
int n = xy.length();
arma::mat Y = arma::zeros<arma::mat>(n,c);
for (int i=0; i<n; i++) {
Y(i,xy[i]-1) = 1;
}
return(Y);
}