- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想了解如何操作特征向量/矩阵。我想实现最小二乘高斯牛顿算法(因此我学习使用 Eigen 库)。我有一个 1x6 的参数 vector ,每次迭代都需要更新它们。现在,我只想弄清楚函数如何将 vector 作为参数并更改其值...
Eigen::VectorXf betas = Eigen::VectorXf::Zero(6);
void SomeFunc(const Eigen::VectorXf& v){ // as per the Eigen guide, one must pass as const
v(0) = 5; // error: expression must be a modifiable lvalue
return;
}
int main()
{
betas(5) = 5.f; // works
SomeFunc(&betas);
std::cout << "Hello World" << std::endl;
std::cout << betas(0) << "\t" << betas(5) << std::endl;
}
我的问题是:如何让一个函数接受一个 vector 并修改它的值?
编辑:Eigen guide
最佳答案
正如文档所说:
Functions taking writable (non-const) parameters must take const references and cast away constness within the function body.
How would you make a function take a vector and modify its values?
您可以使用 const_cast
如下所示:
Eigen::VectorXf betas = Eigen::VectorXf::Zero(6);
void SomeFunc(const Eigen::VectorXf& v){ // as per the Eigen guide, one must pass as const
const_cast<Eigen::VectorXf&>(v)(0) = 5;//cons_cast used here
return;
}
int main()
{
betas(5) = 5.f; // works
SomeFunc(betas);
std::cout << "Hello World" << std::endl;
std::cout << betas(0) << "\t" << betas(5) << std::endl;
}
关于c++ - 采用 VectorXf 并可以修改其值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73320722/
我想了解如何操作特征向量/矩阵。我想实现最小二乘高斯牛顿算法(因此我学习使用 Eigen 库)。我有一个 1x6 的参数 vector ,每次迭代都需要更新它们。现在,我只想弄清楚函数如何将 vect
在 eigen c++ 中,你如何将 vectorXf 映射到 matrixXf(适当的尺寸) (有很好的docs如何为外部对象做所以我知道我们可以做到: MatrixXf x_cen=Map(*x,
我想得到一个未排序的变量的加权中位数长度, Eigen c++ vectorXf 对象。看来我可以使用 boost来自 boost 统计累加器的 weighted_median 函数库来有效地做到这一
我是一名优秀的程序员,十分优秀!