Search code examples
c++c++11eigeneigen3

Setting Eigen Matrix/Vector by index


How exactly do we set the value of an Eigen Vector or Matrix by index. I'm trying to do something similar to:

// Assume row major
matrix[i][j] = value
// or
vector[i] = value

I might have missed it, but could not find anything in the quick reference guide.


Solution

  • As pointed out by user chtz, the problem is the usage of the 'auto' keyword which is further explained on the Eigen website here.

    Both of the following:

    // Assume row major
    matrix(i,j) = value
    // or
    vector(i) = value
    

    should work correctly. I did test on the VectorXf and it indeed works correctly.