[英]How do I remove certain coefficients from a C++ Eigen array, given a boolean vector?
I have a Eigen::VectorXd v
and I would like to create another vector w
containing only the coefficients from v
meeting a certain criteria, like positiveness, something like 我有一个
Eigen::VectorXd v
,我想创建另一个向量w
其中仅包含来自v
满足特定条件的系数,例如正数,类似
Eigen::VectorXd v(3);
v << -1, 0, 1;
w = v(v > 0);
std:: cout << v; // prints -1, 0, 1
std:: cout << w; // prints 1
How can I do this in Eigen? 我该如何在Eigen中做到这一点?
Copy v
in w
, sort w
with std::sort
and then find the first non-positive value with std::find_if
. 复制
w
v
,使用std::sort
排序w
,然后使用std::find_if
查找第一个非正值。 That will be the end of your list of conforming values. 那将是您的符合值列表的结尾。
You can use any other criteria with std::sort
. 您可以将其他任何条件与
std::sort
。
using namespace std;
using namespace Eigen;
template<typename T>
void push_back(VectorXd &v, T val, T ref)
{
if (val > ref /*Or Your condition*/) {
v.conservativeResize(v.size() + 1);
v[v.size() - 1] = val;
}
}
int main()
{
VectorXd v(3);
v << 1, 2, 3;
const double ref = 0.0f;
push_back(v, 4.0, ref);
for (size_t i = 0; i < v.size(); i++)
cout << v(i) << " ";
return 0;
}
You get the idea, you can even templatize the first argument. 您知道了,甚至可以将第一个参数模板化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.