特征:将向量的每个元素与常数进行比较
有没有办法将向量的每个元素与常数进行比较?到目前为止,我正在比较2D矢量eigen :: vector2d
与常数double bolerance
这样:
if (x(0) > tolerance && x(1) > tolerance)
{
...
}
我找到了功能isapprox()
,但是它没有用。关于如何做的是更好或推荐的方法吗?
is there a way how to compare each element of a vector with a constant? So far, I am comparing 2D vector Eigen::Vector2d
with a constant double tolerance
like this:
if (x(0) > tolerance && x(1) > tolerance)
{
...
}
I have found the function isApprox()
but it did not worked somehow. Is there is a nicer or recommended way on how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用
array
vector
类的方法。像这样:在这种情况下,
res
和res2
是cashingbinaryop
,其中包含a
中每个元素的布尔值。使用所有
在两者都是true
时查找。One way to do this is to use the
array
method of theVector
class. Like this:In this case
res
andres2
areCwiseBinaryOp
which contains booleans for each element inA
. Useall
to find when both areTrue
.我觉得这是一个很好的调用是写一个简单的功能:
与@matt的解决方案相比,这种方式的缺点是,对于更复杂的用例,使用特征表达式可以更具性能(不知道,如果,如果这适用于这里)。
这种解决方案的(我认为巨大的)优势是,您
可以准确地看到它的用法所做的。
当然,您还可以将Matts解决方案打包到一个恰当地命名的功能中,以获得此优势。另一个优点是,通过我提供的功能,您确切地知道它的作用,而不必怀疑,是否将自动与特征类型一起使用会咬您。我想不会,马特可能确切地知道为什么。但是我(和你?)不这样做,因此不想依靠它。
I feel like a good call on this one is to write a simple function:
The drawback of this way compared to the solution by @Matt is, that for more complicated use cases, using an Eigen expression can be more performant (no idea, if this applies here).
The (in my opinion huge) advantage of such an solution is, that you
can see exactly what it does from its usage.
Of course you can also pack Matts solution in an aptly named function to get this advantage. Another advantage is, that with the function I provided you know exactly what it does and never have to wonder, whether using auto with an Eigen type could bite you. I guess it won't and Matt probably knows exactly why. But I (and you?) do not and therefore wouldn't want to rely on it.