Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of pointers to Eigen Matrices

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;
      }      
   }
}

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))’

EDIT: output added

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
like image 471
joaocandre Avatar asked May 31 '13 11:05

joaocandre


2 Answers

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;
like image 86
ggael Avatar answered Sep 30 '22 12:09

ggael


Just use operator()(int,int)

CVM[k].operator()(i,j) = 47;

or

CVM[k](i,j) = 47;

or

Eigen::MatrixXd* b = &CVM[k];
b->operator()(i,j) = 47;

Here k is the matrix, i is the row, and j is the column.

like image 43
Justin Lye Avatar answered Sep 30 '22 13:09

Justin Lye