从特征求解器中的矢量中检索值
retrieving values from Vector in Eigen Solver
我正在使用特征求解器。我无法从我创建的矢量/矩阵中检索值。例如,在下面的代码中,我没有错误,但收到运行时错误。
#include <iostream>
#include <math.h>
#include <vector>
#include <EigenDense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix3f A;
Vector3f b;
vector<float> c;
A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
b << 3, 3, 4;
cout << "Here is the matrix A:n" << A << endl;
cout << "Here is the vector b:n" << b << endl;
Vector3f x = A.colPivHouseholderQr().solve(b);
for (int i = 0; i < 3; i++)
{
c[i] = x[i];
cout << c[i] << " ";
}
//cout << "The solution is:n" << x << endl;
return 0;
}
如何将 x 中的值检索到我选择的变量(我需要这个,因为这将是我编写的另一个函数中的一个参数(。
使用
vector<float> c(3);
或
for (int i = 0; i < 3; i++)
{
c.push_back(x[i]);
cout << c[i] << " ";
}
如评论中所述,问题在于c
在为其分配值之前没有调整其大小。此外,您实际上不需要Eigen::Vector3f x
,但您可以将.solve()
操作的结果直接分配给指向vector
数据的Map
:
#include <iostream>
#include <vector>
#include <Eigen/QR>
using namespace Eigen;
using namespace std;
int main()
{
Matrix3f A;
Vector3f b;
vector<float> c(A.cols());
A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
b << 3, 3, 4;
cout << "Here is the matrix A:n" << A << endl;
cout << "Here is the vector b:n" << b << endl;
Vector3f::Map(c.data()) = A.colPivHouseholderQr().solve(b);
for(int i=0; i<3; ++i) std::cout << "c[" << i << "]=" << c[i] << 'n';
}
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 使用VerQueryValue检索应用程序的文件描述
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 是否可以从格式字符串中检索"width"
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 特征命名访问向量段
- 将特征矩阵的向量设置为0
- 特征:模板函数中矩阵的平面图
- basic_string的前导/尾部不区分空格的特征
- 特征 3 类的模板专用化
- 特征 c++:复矩阵的面积双曲正切(atanh)
- C++ 中的特征向量计算
- 根据C++标准的定义实现"is_similar"类型特征
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- 有没有办法找到特征矩阵系数的中值?
- 如何将高维数据映射到特征类型?
- 将平面阵列重塑为复杂的特征类型
- 特征 LLT 模块给出不正确的结果?
- 从特征求解器中的矢量中检索值