1
votes

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.

2
The guide seems to use matrix(i,j) or vector(i), if that's what you're looking for.frslm
matrix(i,j) and vector[i] should be used.Ali250
auto train_loss_avg = Eigen::VectorXf::Zero(20); train_loss_avg(0) = 3.0; is giving expression not assignable.tangy
fyi even train_loss_avg[0] = 3.0; throws the same error.tangy
Don't assign your Zero vector to an auto type. See Common pitfallschtz

2 Answers

4
votes

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.

1
votes

Block operation is one choice:

Eigen::Vector4f diag_Vec(1, 2, 4, 7);
Eigen::Matrix4f Mat = diag_Vec.matrix().asDiagonal();
Mat.block<1, 1>(2, 3) = Eigen::Matrix<float, 1, 1>(-4.5);
Mat.block<1, 1>(3, 2) = Eigen::Matrix<float, 1, 1>(1);
cout << "Mat: \n" <<Mat << endl;