我是靠谱客的博主 温婉水池,这篇文章主要介绍Eigen 矩阵Matrix及其简单操作1. Matrix类2. Vectors向量,现在分享给大家,希望可以做个参考。

1. Matrix类

在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vector只是一种特殊的矩阵(一行或者一列)。
Matrix有6个模板参数,主要使用前三个参数,剩下的有默认值。

Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

Scalar是表示元素的类型,RowsAtCompileTime为矩阵的行,ColsAtCompileTime为矩阵的列。

库中提供了一些类型便于使用,比如:

typedef Matrix<float, 4, 4> Matrix4f;

2. Vectors向量

列向量

typedef Matrix<float, 3, 1> Vector3f;

行向量

typedef Matrix<int, 1, 2> RowVector2i;
一、矩阵的定义
Matrix<double, 3, 3> A;                // A.定义3x3double 矩阵
Matrix<double, 3, Dynamic> A;          //定义3xn double 矩阵,列为动态变化
Matrix<double, Dynamic, Dynamic>  A;   // 定义 double 矩阵,行、列为动态变化,由需要决定
MatrixXd A;                            // 定义 double 矩阵,行、列为动态变化,由需要决定
Matrix<double, 3, 3, RowMajor>  A;     // 定义3x3 double 矩阵,按行储存,默认按列储存效率较高。
Matrix3f  A;                           // 定义3x3 float 矩阵A.
Vector3f  A;                           // 定义3x1 float 列向量A.
VectorXd  A;                           // 定义动态double列向量A
RowVector3f  A;                        // 定义1x3 float 行向量A.
RowVectorXd  A;                        // 定义动态double行向量A.

二、基础用法
// Eigen        // Matlab           // comments
A.size()        // length(x)        // 元素个数
C.rows()        // size(C,1)        // 行个数
C.cols()        // size(C,2)        // 列个数

A(i)            // x(i+1)           // 默认情况下列优先,访问(0,i)的元素
C(i, j)         // C(i+1,j+1)       //访问(i, j)的元素。
A.resize(4, 4);       // 运行时,如果之前已经定义过形状则会报错。
B.resize(4, 9);       // 运行时,如果之前已经定义过形状则会报错。
A.resize(3, 3);       // Ok; size didn't change.
B.resize(3, 9);       // Ok; only dynamic cols changed.
 
A << 1, 2, 3,     // Initialize A. The elements can also be
     4, 5, 6,     // matrices, which are stacked along cols
     7, 8, 9;     // and then the rows are stacked.
B << A, A, A;     // B is three horizontally stacked A's.
A.fill(10);       // Fill A with all 10's.

三、特殊矩阵定义
// Eigen                                                
//单位矩阵定义
MatrixXd::Identity(rows,cols)       
C.setIdentity(rows,cols)            
//零矩阵定义
MatrixXd::Zero(rows,cols)          
C.setZero(rows,cols)              
//全1矩阵定义
MatrixXd::Ones(rows,cols)         
C.setOnes(rows,cols)               
//随即矩阵定义
MatrixXd::Random(rows,cols)            
C.setRandom(rows,cols)          
//线性阵定义
VectorXd::LinSpaced(size,low,high)  
v.setLinSpaced(size,low,high)     

四、矩阵分块
// 下面x为列或行向量,P为矩阵
**************************只能对向量操作************************************
x.head(n)                          // 列向量的前n个元素
x.head<n>()                        // 行向量的前n个元素
x.tail(n)                          // 列向量的倒数n个元素
x.tail<n>()                        // 行向量的倒数n个元素
x.segment(i, n)                    // 行向量从i开始的n个元素
x.segment<n>(i)                    // 列向量从i开始的n个元素
**************************只能对矩阵操作******************************************
P.block(i, j, rows, cols)          // 从i行j列开始的rows行cols列块。
P.block<rows, cols>(i, j)          //  从i行j列开始的rows行cols列块
P.row(i)                           // 矩阵P的第i行元素
P.col(j)                           // 矩阵P的第j列元素
P.leftCols<cols>()                 // P矩阵左边cols列元素
P.leftCols(cols)                   //P矩阵左边cols列元素
P.middleCols<cols>(j)              // P矩阵第j列开始的cols列元素
P.middleCols(j, cols)              // P矩阵第j列开始的cols列元素
P.rightCols<cols>()                // P矩阵右边cols列元素
P.rightCols(cols)                  // P矩阵右边cols列元素
P.topRows<rows>()                  // P矩阵前rows行元素
P.topRows(rows)                    // P矩阵前rows行元素
P.middleRows<rows>(i)              // P矩阵第i行开始的row行元素
P.middleRows(i, rows)              // P矩阵第i行开始的row行元素
P.bottomRows<rows>()               // P矩阵倒数row行
P.bottomRows(rows)                 // P矩阵倒数row行
P.topLeftCorner(rows, cols)        // P矩阵左上角rows行,cols列元素
P.topRightCorner(rows, cols)       // P矩阵右上角rows行,cols列元素
P.bottomLeftCorner(rows, cols)     
P.bottomRightCorner(rows, cols)    
P.topLeftCorner<rows,cols>()       
P.topRightCorner<rows,cols>()      
P.bottomLeftCorner<rows,cols>()    
P.bottomRightCorner<rows,cols>()   

五、矩阵元素交换                      
R.row(i) = P.col(j);    //可以将P的列元素去替换R的行元素
R.col(j1).swap(P.col(j2));  //将P的列元素和R的列元素进行互换


六、矩阵转置                       
R.adjoint()                        // R矩阵的伴随矩阵
R.transpose()                      // R矩阵的转置
R.diagonal()                       // R矩阵的迹,用列表示
x.asDiagonal()                     // 对角矩阵
R.reverse()                        // R矩阵逆时针旋转180度(反转)
R.colwise().reverse(); 			// R矩阵的列反转
R.rowwise().reverse(); 			// R矩阵的行反转
R.transpose().colwise().reverse(); // R矩阵逆时针旋转90度
R.transpose().rowwise().reverse(); // R矩阵顺时针旋转90度
R.conjugate()                      // conj(R)共轭矩阵


七、矩阵乘积
// Matrix-vector.  Matrix-matrix.   Matrix-scalar.
y  = M*x;          R  = P*Q;        R  = P*s;
a  = b*M;          R  = P - Q;      R  = s*P;
a *= M;            R  = P + Q;      R  = P/s;
                   R *= Q;          R  = s*P;
                   R += Q;          R *= s;
                   R -= Q;          R /= s;

八、矩阵内部元素操作
// Vectorized operations on each element independently
// Eigen                  // Matlab
R = P.cwiseProduct(Q);    // R = P .* Q对应点乘
R = P.array() * s.array();// R = P .* s对应点乘
R = P.cwiseQuotient(Q);   // R = P ./ Q对应点除
R = P.array() / Q.array();// R = P ./ Q对应点除
R = P.array() + s.array();// R = P + s  对应点加
R = P.array() - s.array();// R = P – s  对应点减
R.array() += s;           // R = R + s
R.array() -= s;           // R = R - s
R.array() < Q.array();    // R < Q  //Q矩阵元素比较,会在相应位置置0或1
R.array() <= Q.array();   // R <= Q //Q矩阵元素比较,会在相应位置置0或1
R.cwiseInverse();         // 1 ./ P   //1点除以P
R.array().inverse();      // 1 ./ P   //1点除以P
R.array().sin()           // sin(P)
R.array().cos()           // cos(P)
R.array().pow(s)          // P .^ s
R.array().square()        // P .^ 2
R.array().cube()          // P .^ 3
R.cwiseSqrt()             // sqrt(P)
R.array().sqrt()          // sqrt(P)
R.array().exp()           // exp(P)
R.array().log()           // log(P)
R.cwiseMax(P)             // max(R, P) //max(R, P) 对应取大
R.array().max(P.array())  // max(R, P) //min(R, P) 对应取小
R.cwiseMin(P)             // min(R, P)
R.array().min(P.array())  // min(R, P)
R.cwiseAbs()              // abs(P)
R.array().abs()           // abs(P)
R.cwiseAbs2()             // abs(P.^2)
R.array().abs2()          // abs(P.^2)
(R.array() < s).select(P,Q);  // (R < s ? P : Q)
附:
要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;

要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;

要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;

通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。

要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。

要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。

最后

以上就是温婉水池最近收集整理的关于Eigen 矩阵Matrix及其简单操作1. Matrix类2. Vectors向量的全部内容,更多相关Eigen内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部