Basic question from a newcomer to Armadillo and C++ from R.
I have a vector X
and I want to set the entries below 0
to a given value and the ones larger than 0
to another. Armadillo has the find
function for returning indices of elements of X that are non-zero or satisfy a relational condition (not logical!?) so I can do:
arma::uvec ind0 = find(X < 0);
arma::uvec ind1 = find(X >= 0);
X(ind0).zeros();
X(ind1).fill(1);
This is clearly not the best solution. What would be a better way that does not involve calling find
two times?
You can use the .transform() member function. Requires C++11 compiler.
mat X(100,100,fill::randu);
X -= 0.5;
X.transform( [](double val) { return (val < 0) ? double(0) : double(1); } );