Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(Rcpp)
- sourceCpp(code = '
- #include <RcppArmadillo.h>
- // [[Rcpp::depends(RcppArmadillo)]]
- using namespace Rcpp;
- using namespace arma;
- // [[Rcpp::export]]
- NumericMatrix bsxfun_Rcpp(Function f, mat A, mat B){
- uword Nrow = (A.n_rows > B.n_rows)? A.n_rows : B.n_rows,
- Ncol = (A.n_cols > B.n_cols)? A.n_cols : B.n_cols;
- return as<NumericMatrix>( f(
- as<NumericMatrix>( wrap(repmat(A, Nrow / A.n_rows, Ncol / A.n_cols))),
- as<NumericMatrix>( wrap(repmat(B, Nrow / B.n_rows, Ncol / B.n_cols)))
- ));
- }')
- A = matrix(rnorm(4), 4, 1)
- B = matrix(rnorm(8), 4, 2)
- bsxfun_Rcpp(function(x, y) x + y, B, A)
- sweep(B, 1, A, '+')
- bsxfun_Rcpp(pmin, B, A)
- sweep(B, 1, A, pmin)
- bsxfun_Rcpp(pmax, B, A)
- sweep(B, 1, A, pmax)
Advertisement
Add Comment
Please, Sign In to add comment