简体   繁体   中英

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 .

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM