2

I need to create a MatrixXd combining multiple VectorXds as columns. I do not know the number of rows in the vector at the compile time (here I used 10 as size to show my problem)

I referred here and here I have the following codes

VectorXd vectOne= VectorXd(10);
VectorXd vectTwo = VectorXd(10);
VectorXd vectThree = VectorXd(10);

for (int i = 0; i < 10; i++) {
        vectOne(i, 0) = i*2;
        vectTwo(i, 0) = i*3;
        vectThree(i, 0) = i*4;
}

        MatrixXd m(10, 3);

//method 1 - FAILED
        m.col(0) = vectOne;
        m.col(1) = vectTwo;
        m.col(2) = vectThree;

//method 2 -FAILED
        m<< vectOne, vectTwo, vectThree;
    

Both do not work. I get errors like below.

Method 1 error: Assertion failed: startRow >= 0 && blockRows >= 0 && startRow <= xpr.rows() - blockRows && startCol >= 0 && blockCols >= 0 && startCol <= xpr.cols() - blockCols

Method 2 error: Assertion failed: rows == this->rows() && cols == this->cols() && "DenseBase::resize() does not actually allow to resize."

Please help to solve this.

Community
  • 1
  • 1
AStudent
  • 49
  • 1
  • 2
  • 2
    Method 1 works fine for me. I copy-pasted your code. Are you using the latest version of Eigen? You could try without the extra 0's (vectOne(i) = i*2), also works for me. – Burrito Jan 12 '18 at 17:57
  • It does not work for me :-( I downloaded the latest Eigen and I'm on Windows with Visual Studio. – AStudent Jan 12 '18 at 17:59
  • 2
    Both methods work fine for me using various versions of Eigen. – ggael Jan 12 '18 at 18:07
  • Me too, on Visual Studio 2017. You have #include? – Burrito Jan 12 '18 at 18:10
  • @Benitok yes I have it too. :-( – AStudent Jan 12 '18 at 18:13
  • Everything is ok until I reach `m.col(0) = vectOne` or `m<< vectOne, vectTwo, vectThree` – AStudent Jan 12 '18 at 18:14
  • The code you have posted works. In both cases the correct matrix is generated. Cannot reproduce the error message. I suspect that the error is elsewhere in the code, or that your Eigen installation is broken. – RHertel Jan 12 '18 at 18:39
  • Thank you everyone. Since you say the code works for everyone it should be somewhere else as @RHertel says. I will check the code again and see. Thank you for checking my code. :-) – AStudent Jan 12 '18 at 19:02
  • 1
    I fixed it! The error was somewhere else actually and this code does not show that. I actually have doubles in my vectors and when I define the matrix as MatrixXd as I have shown, it gave the error. So I created a new type with typedef that holds doubles. Then it worked! I am thankful to each of you for testing the code and telling error is somewhere else. – AStudent Jan 13 '18 at 00:58

0 Answers0