= <= using Eigen 3? I am porting some MATLAB code to C ++ using the Eigen 3 template libr...">

How to express "<array-of-true-or-false> = <array> <= <scalar> using Eigen 3?

I am porting some MATLAB code to C ++ using the Eigen 3 template library, and I am looking for a good mapping for this common MATLAB idiom:

 K>> [1 2 3 4 5] <= 3 ans = 1 1 1 0 0 

So, compare the array and scalar, returning an array of logical elements that have the same shape.

I understand that the Eigen Array class has a comparison operator for coefficients, but if I interpret the documents correctly, they only work with another array; not with scalar values.

Is there any option that I missed that will perform a comparison with a scalar? Or, if it is not, a good idiomatic way to create an Array filled with a scalar value for an RHS expression?

+6
source share
1 answer

Thanks to ChriSopht _ from the #eigen IRC channel:

 VectorXd compareMat = ...; double cutoff = 3; Matrix<bool, Dynamic, 1> result = compareMat.array() <= cutoff; 

So, the trick uses .array() to get the operators with a coefficient, and, of course, after that the correct return type ...

+7
source

All Articles