4

I have to fill a vector with values within a for loop using EIGEN. Im trying something like that...

#include <iostream> 
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;

int main(){ 
  VectorXd s;
  for (int i=0,i<10;i++){
     s(i) = (double) i;
  }
return 0;
}

i can compile it, but when i run the program i get this error:

dynamic: /usr/include/Eigen/src/Core/DenseCoeffsBase.h:425: Eigen::DenseCoeffsBase::Scalar& Eigen::DenseCoeffsBase::operator()(Eigen::Index) [with Derived = Eigen::Matrix; Eigen::DenseCoeffsBase::Scalar = double; Eigen::Index = int]: Assertion `index >= 0 && index < size()' failed. Abgebrochen

I know that i can easily achieve that using the std::vector class, but i want to do it with eigen because i have to do a lot of matrix operations after that.

Thank you!

EDIT: for my application i don't know the size of the vector at compile time. I want to find out whether there is any similar method like vector::push_back in eigen.

1
  • Assertion index >= 0 && index < size() failed. You can easily do the same out-of-bounds access with std::vector.
    – LogicStuff
    Commented Jan 26, 2017 at 8:04

2 Answers 2

8

You forgot to reserve space for the vector. This would be same for std::vector.

Try this

#include <Eigen/Dense>
#include <iostream>

int main()
{
  // resize to 10 elements
  auto vec = Eigen::VectorXd(10);
  for (int i = 0; i < vec.size(); ++i) {
    vec[i] = i;
  }
  std::cout << vec << '\n';
}
2
  • 1
    That's the problem, that i dont know the size of the vector at compile time. With std::vector i can do that with the push_back method. Is there any way to achieve the same with eigen? Thanks!
    – elToro
    Commented Jan 26, 2017 at 8:15
  • You could use the the resize function. See eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
    – Maikel
    Commented Jan 26, 2017 at 9:18
0

As @Maikel advised me. I could solve my problem with the resize function as follows;

#include <iostream> 
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;

int main(){ 
  VectorXd s;
  for (int i=0,i<10;i++){
     s.resize(i+1);
     s(i) = (double) i;
  }
return 0;
} 

@Jonas made me realize that I know the size of the vector before the loop, so I can resize once before the loop.

About the resize function in the documentation in Eigen could be found:

"The resize() method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change. If you want a conservative variant of resize() which does not change the coefficients, use conservativeResize() "

therefore, when it could be better to use conservativeResize() to assure the values wont be lost.

3
  • 1
    I am not sure if the resize function guarantees to keep the old values. Do you have no way of telling how much you are going to need in runtime?
    – Maikel
    Commented Jan 26, 2017 at 9:40
  • when do you have the info for the size of the vector? do you need to resize in a loop or can u do it before it? resizing in a loop is usually inefficient.
    – jonas_toth
    Commented Jan 26, 2017 at 9:47
  • I know the size of the vector before the loop, that is a great point. Thank you!
    – elToro
    Commented Jan 26, 2017 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.