joa*_*dre 5 c++ arrays pointers matrix eigen
我在我的代码上使用了Eigen的MatrixXd矩阵,在某个时刻我需要一个3D矩阵.由于Eigen不具有三维矩阵类型,因为它仅针对线性代数进行了优化,而是创建了MatrixXd类型的指针数组:
Eigen::MatrixXd *CVM =new Eigen::MatrixXd[100];
for (int i = 0; i < 100; i++){
CVM[i]= Eigen::MatrixXd::Zero(5,5);
}
Run Code Online (Sandbox Code Playgroud)
但是,稍后我需要访问此数组的值,为此我做的事情如下:
for (int k = 0; k < 100; k++){
Eigen::MatrixXd* b=&CVM[k];
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
b->coeff(i,j)=47;
}
}
}
Run Code Online (Sandbox Code Playgroud)
作为b一个指针,而不是它MatrixXd自己,b(i,j)显然不会工作,所以我使用该coeff()方法,但是,我收到以下错误:
error: assignment of read-only location ‘b->Eigen::Matrix<double, -1, -1>::<anonymous>.Eigen::PlainObjectBase<Derived>::coeff<Eigen::Matrix<double, -1, -1> >(((Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1> >::Index)i), ((Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1> >::Index)j))’
Run Code Online (Sandbox Code Playgroud)
编辑:输出添加
cout << b << endl;
cout << CVM[47] << endl;
0x1c34b00
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
要么使用b->coeffRef(i,j)获取读/写引用,要么取消引用b : (*b)(i,j),或使用b的引用:
MatrixXd& b = CVM[k];
b(i,j) = 47;
Run Code Online (Sandbox Code Playgroud)