与matlab对比
eigen就是一个matrix的模板类。
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
class Matrix
我们一般用前面三个参数,_Scalar即不同的数据类型(int float double complex等),所谓的模板,也就是这个。
Vecotor3d = Matrix<double,3,1>
MatrixXf = Matrix<float, Dynamic, Dynamic>
- 初始化与赋值
//1. 直接初始化
Vector3d a(0,0,1); //这样不能在作为类的成员变量时使用
Vector3d a = {0,0,1}; //这样也可以
Vector3d{0,0,1} ;//不要变量名
//2.先定义再赋值
Vector3d a;
a << 0,0,1;
a = Vector3d{0,0,1} //这里又生成了一个临时变量
a = {0,0,1} //这样也可以
//3.也可以用下面元素访问的形式赋值
a(0) = 0;
m(0,1) =1;
m.row(0) = Vector3d{0,0,1};
- 元素的访问
//1.使用常规的下标
v[0] = 1;
v(1) = 2;
m(0,0) =3;
//2.matrix读取某行某列
m.col(0)
m.row(1)
//3.访问矩阵块
// Eigen // Matlab
x.head(n) // x(1:n)
x.head<n>() // x(1:n)
x.tail(n) // x(end - n + 1: end)
x.tail<n>() // x(end - n + 1: end)
x.segment(i, n) // x(i+1 : i+n)
x.segment<n>(i) // x(i+1 : i+n)
P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols)
P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols)
P.row(i) // P(i+1, :)
P.col(j) // P(:, j+1)
P.leftCols<cols>() // P(:, 1:cols)
P.leftCols(cols) // P(:, 1:cols)
P.middleCols<cols>(j) // P(:, j+1:j+cols)
P.middleCols(j, cols) // P(:, j+1:j+cols)
P.rightCols<cols>() // P(:, end-cols+1:end)
P.rightCols(cols) // P(:, end-cols+1:end)
P.topRows<rows>() // P(1:rows, :)
P.topRows(rows) // P(1:rows, :)
P.middleRows<rows>(i) // P(i+1:i+rows, :)
P.middleRows(i, rows) // P(i+1:i+rows, :)
P.bottomRows<rows>() // P(end-rows+1:end, :)
P.bottomRows(rows) // P(end-rows+1:end, :)
P.topLeftCorner(rows, cols) // P(1:rows, 1:cols)
P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end)
P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols)
P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end)
P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols)
P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end)
P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols)
P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end)
- 基本操作
//1.对应元素的加减
m = m1 + m2
//2.乘 (矩阵的相乘)
m = m1*m2
//3.向量的点成v. dot(v1) 叉乘 v.cross(v2)
//4.转置 transpose()
//5.求模 norm()
//6. a.sum(): 返回矩阵a中所有元素的和
a.prod(): 返回矩阵a中所有元素的积
a.mean(): 返回矩阵a中所有有元素的平均值
a.trace(): 返回矩阵的迹,即返回主对角线上元素的和。如果不是方阵或者为向量,仍返回对角线元素的和。
a.minCoeff(): 返回矩阵中最小的元素
a.maxCoeff(): 返回矩阵中最大的元素
//另外,对于.minCoeff(),.maxCoeff(),有以下用法:
int i,j;//或std::ptrdiff_t i, j
auto min = a.minCoeff(i,j);返回a的最小元素赋值给min,并将最小元素所在行号、列号赋值给i、j。
auto max = a.maxCoeff(p,q);返回a的最大元素赋值给max,并将最大元素所在行号、列号赋值给q、p。
4.生成特殊矩阵
// Eigen // Matlab
MatrixXd::Identity(rows,cols) // eye(rows,cols)
C.setIdentity(rows,cols) // C = eye(rows,cols)
MatrixXd::Zero(rows,cols) // zeros(rows,cols)
C.setZero(rows,cols) // C = ones(rows,cols)
MatrixXd::Ones(rows,cols) // ones(rows,cols)
C.setOnes(rows,cols) // C = ones(rows,cols)
MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 // MatrixXd::Random returns uniform random numbers in (-1, 1).
C.setRandom(rows,cols) // C = rand(rows,cols)*2-1
VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)'
v.setLinSpaced(size,low,high) // v = linspace(low,high,size)'