How do I express “<array-of-true-or-false> = <array> <= <scalar>” using Eigen 3?

自古美人都是妖i 提交于 2019-12-01 17:00:45

问题


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 an array and a scalar, returning an array of booleans that has the same shape.

I understand that Eigen's Array class has coefficient-wise comparison operators, but if I'm interpreting the docs correctly they only work with another array; not with scalar values.

Is there some option I've missed that will perform the comparison with a scalar? Or failing that, a nice idiomatic way to create an appropriately-shaped Array filled with the scalar value for the RHS of the expression?


回答1:


With thanks to ChriSopht_ from the #eigen IRC channel:

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

So, the trick is using .array() to get at coefficient-wise operators, and of course then getting the return type right…



来源:https://stackoverflow.com/questions/15938229/how-do-i-express-array-of-true-or-false-array-scalar-using-eigen-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!