This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to store and access dynamic MatrixXd in a std::vector?

Tags: None
(comma "," separated)
hsharma
Registered Member
Posts
11
Karma
0
Hi,
I wanted to store a dynamic MatrixXd in a std::vector. I tried this:

std::vector<Eigen::MatrixXd,Eigen::aligned_allocator<Eigen::MatrixXd> > mat;

Eigen::MatrixXd m(9,1);

for(int i = 0; i < 8; ++i)
{
m(0,0) = val;
m(1,0) = val;
.................

mat.pushback(m);
}

when I am checking the size of mat I am getting 16 instead of 8.
and when I am printing mat.at(0) it doesn't print anything.

Please help me with this soon.

Thanks in advance.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I don't understand your issue, here is a simple working example:
Code: Select all
#include <Eigen/Dense>
#include <vector>
using namespace Eigen;
int main()
{
  std::vector<MatrixXd> mat;

  Eigen::MatrixXd m(9,1);

  for(int i = 0; i < 8; ++i)
  {
    m << i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8;
    mat.push_back(m);
  }
  std::cout << mat.at(0).rows() << " x " << mat.at(0).cols() << " :\n" <<  mat.at(0) << "\n\n";
  std::cout << mat.at(7).rows() << " x " << mat.at(7).cols() << " :\n" <<  mat.at(7) << "\n";
}


btw, aligned_allocator is needed for fixed size types only.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]