123456789101112131415161718192021
#include <iostream> #include "Eigen/Dense" using Eigen::MatrixXd; int main() { MatrixXd G(4,3); // global matrix G << 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10,0.11,0.12; Eigen::Ref<Eigen::MatrixXd> A = G.col(1); A.row(2) << 0.99; std::cout << G; }
1234
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.99 0.9 0.1 0.11 0.12
12
Eigen::Ref<Eigen::MatrixXd> A = G.row(1); A.col(2) << 0.99;
error: conversion from 'Eigen::DenseBase<Eigen::Matrix<double, -1, -1> >::RowXpr' {aka 'Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>'} to non-scalar type 'Eigen::Ref<Eigen::Matrix<double, -1, -1> >' requested
Eigen::MatrixXd& A = G.col(1);
Non-const lvalue reference to type 'Eigen::MatrixXd' (aka 'Matrix<double, Dynamic, Dynamic>') cannot bind to a temporary of type 'Eigen::DenseBase<Eigen::Matrix<double, -1, -1>>::ColXpr' (aka 'Block<Eigen::Matrix<double, -1, -1, 0, -1, -1>, internal::traits<Matrix<double, -1, -1, 0, -1, -1>>::RowsAtCompileTime, 1, !IsRowMajor>')
123456789101112131415
/* You can access elements of vectors and matrices using normal subscripting: * * \code * Eigen::VectorXd v(10); * v[0] = 0.1; * v[1] = 0.2; * v(0) = 0.3; * v(1) = 0.4; * * Eigen::MatrixXi m(10, 10); * m(0, 1) = 1; * m(0, 2) = 2; * m(0, 3) = 3; * \endcode */
col()
row()
1234567
MatrixXd G(4,3); MatrixXd& ref = G; ref(1,1) = 0; // or auto& MyRef = G(1,1);