问题描述
我在代码上使用了Eigen的MatrixXd矩阵,在某个时候我需要3D矩阵.由于Eigen没有三维矩阵类型,因为它仅针对线性代数进行了优化,所以我创建了MatrixXd类型的指针数组:
I am using MatrixXd matrices from Eigen on my code, and at a certain point I need a 3D one. Since Eigen does not have tridimensional matrix types, as it is optimized just for linear algebra, instead I am creating a pointer array of the MatrixXd type:
Eigen::MatrixXd *CVM =new Eigen::MatrixXd[100];
for (int i = 0; i < 100; i++){
CVM[i]= Eigen::MatrixXd::Zero(5,5);
}
但是,稍后我需要访问该数组上的值,为此,我正在做类似的事情:
However, later on I need to acess the values on this array, and for that I am doing something like:
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;
}
}
}
由于b
是指针而不是MatrixXd
本身,所以b(i,j)
显然不起作用,所以我改用coeff()
方法,但是,出现以下错误:
As b
is a pointer and not the MatrixXd
itself, b(i,j)
obviously wouldn't work, so instead I am using the coeff()
method, however, I get the following error:
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))’
添加了输出
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
推荐答案
要么使用b->coeffRef(i,j)
获取读/写引用,要么取消引用b:(*b)(i,j)
,或者为b使用引用:
Either use b->coeffRef(i,j)
to get a read/write reference, or dereference b: (*b)(i,j)
, or use a reference for b:
MatrixXd& b = CVM[k];
b(i,j) = 47;
这篇关于指向本征矩阵的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!