矩阵从标准矢量到矢量从特征/密集(Matrix from std vector to vector from Eigen/Dense)

编程入门 行业动态 更新时间:2024-10-26 10:32:16
矩阵从标准矢量到矢量从特征/密集(Matrix from std vector to vector from Eigen/Dense)

我尝试从使用std c ++动态容器为向量创建的3D矩阵中读出元素。 以下是我初始化矩阵的方法:

typedef vector<vector<vector<ClassA> > > matrix3D;

在我的名为“ClassA”的班级中,我有以下公共成员:

double a, b, c;

然后在我的主文件中,我填写矩阵:

double varA=M_PI; double varB=varA; double varC=varA;

matrix3D[i][j][k].a = varA;

matrix3D[i][j][k].b = varB;

matrix3D[i][j][k].c = varC;

现在,当我将双打读入使用Eigen / Dense库创建的向量时,向量的类型变为矩阵:

Vector3d vectorEigen; vectorEigen << matrix3D[i][j][k].a, matrix3D[i][j][k].b, matrix3D[i][j][k].c;

和vectorEigen成为Eigen::Matrix<double, 3,1,0,3,1>类型的变量

有没有人知道我错过了什么?

I try to read out elements from a 3D matrix created using the std c++ dynamic container for vectors. Below is how I initialize my matrix:

typedef vector<vector<vector<ClassA> > > matrix3D;

In my class named "ClassA", I have the following public members:

double a, b, c;

Then in my main file, I fill in the matrix with:

double varA=M_PI; double varB=varA; double varC=varA;

matrix3D[i][j][k].a = varA;

matrix3D[i][j][k].b = varB;

matrix3D[i][j][k].c = varC;

Now when I read the doubles into a vector created using Eigen/Dense library, the type of the vector becomes a matrix:

Vector3d vectorEigen; vectorEigen << matrix3D[i][j][k].a, matrix3D[i][j][k].b, matrix3D[i][j][k].c;

and vectorEigen becomes a variable of the type Eigen::Matrix<double, 3,1,0,3,1>

Does anybody have a clue what I have missed here?

最满意答案

内部Eigen将向量表示为仅具有一列的矩阵。 因此,矢量(就像“普通”矩阵)实际上是Eigen::Matrix模板类的实例。

然而,为了简化程序员,Eigen使用C ++的typedef来定义矢量类,这些类是具有特定选项的Eigen::Matrix<>同义词。 例如, Eigen的Vector3d类型是矩阵的typedef ,其元素为double s且有3行1列:

typedef Matrix<double, 3, 1> Vector3d

Matrix模板类实际上有6个模板参数,最后3个是默认参数。 这是完整的签名:

template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> class Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols >

如果编译器在错误消息中引用Eigen::Matrix<double, 3,1,0,3,1> ,那么它正在讨论具有以下模板参数的Eigen::Matrix :

_Scalar = double _Rows = 3 _Cols = 1 _Options = 0(默认情况下) _MaxRows = _Rows (默认情况下)= 3 _MaxCols = _Cols (默认情况下)= 1

所以Eigen::Matrix<double, 3,1,0,3,1>只是编译器在解析typedef和template参数后看到的完整类型的Vector3d 。

类型根本没有改变,你只需在代码中使用Vector3d简写符号,而编译器通过其显式类型引用它。

Internally Eigen represents vectors as matrices with only one column. So vectors (just like "ordinary" matrices) are really instances of an Eigen::Matrix template class.

For simplicity towards the programmer however, Eigen uses C++'s typedef to define vector classes that are synonyms for an Eigen::Matrix<> with specific options. For example, the Vector3d type in Eigen is a typedef for a matrix whose elements are doubles and that has 3 rows and 1 column:

typedef Matrix<double, 3, 1> Vector3d

The Matrix template class actually has 6 template arguments, the last 3 ones being default arguments. Here is the full signature:

template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> class Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols >

If the compiler refers to Eigen::Matrix<double, 3,1,0,3,1> in error messages it's talking about an Eigen::Matrix with the following template parameters:

_Scalar= double _Rows = 3 _Cols = 1 _Options = 0 (by default) _MaxRows = _Rows (by default) = 3 _MaxCols = _Cols (by default) = 1

So Eigen::Matrix<double, 3,1,0,3,1> is just the full type of Vector3d that the compiler sees after resolving typedef and template arguments.

The type hasn't changed at all, you just use the Vector3d shorthand notation in your code, whereas the compiler refers to it by its explicit type.

更多推荐

本文发布于:2023-07-09 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1086992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矢量   矩阵   密集   特征   标准

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!