Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 240
tangy Avatar asked Sep 03 '25 06:09

tangy


2 Answers

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.

like image 107
tangy Avatar answered Sep 04 '25 19:09

tangy


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;
like image 25
Zimeng Zhao Avatar answered Sep 04 '25 18:09

Zimeng Zhao