1 year ago

#363689

test-img

NickSc79

Scale the columns of an RcppArmadillo matrix using lambda function

I want to min-max the columns of a matrix using RcppArmadillo and a lambda function, without using a for loop. I have tried this in the code below but this just returns the original matrix. How to replace the original values with the scaled values?

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins("cpp11")]]

using namespace Rcpp;
using namespace arma;

//[[Rcpp::export]]
arma::mat min_maxC(arma::mat X) {
   arma::mat Xsc(size(X));
   Xsc = X.each_col( [] (arma::vec& x) { return(x - min(x))/(max(x) - min(x)); });
   return Xsc;
}

/***R
X = matrix(sample(100,50), 5,10)
min_maxC(X)
*/

r

lambda

rcpp

rcpparmadillo

0 Answers

Your Answer

Accepted video resources