What is the best way to perform modulus operator over a vector or matrix in c++ Armadillo?
The vector and matrix classes overload the % operator to perform element-wise multiplication. Trying to use it yields an invalid operands error. I was expecting that
uvec a = {0, 1, 2, 3};
uvec b = a % 2;
cout << "b" << endl;
would yield the following:
b:
0
1
0
1
Operator '%' is for element-wise matrix multiplication. You have to create your own function:
/**
* Extend division reminder to vectors
*
* @param a Dividend
* @param n Divisor
*/
template<typename T>
T mod(T a, int n)
{
return a - floor(a/n)*n;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With