简体   繁体   English

从标准向量到本征/密度向量的矩阵

[英]Matrix from std vector to vector from Eigen/Dense

I try to read out elements from a 3D matrix created using the std c++ dynamic container for vectors. 我尝试从使用std c ++动态容器创建矢量的3D矩阵中读取元素。 Below is how I initialize my matrix: 以下是我初始化矩阵的方式:

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

In my class named "ClassA", I have the following public members: 在名为“ ClassA”的班级中,我具有以下公共成员:

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: 现在,当我将双打读入使用Eigen / Dense库创建的向量中时,向量的类型变为矩阵:

    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> 并且vectorEigen变成类型为Eigen::Matrix<double, 3,1,0,3,1>的变量

Does anybody have a clue what I have missed here? 有人知道我在这里错过了什么吗?

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. 因此,向量(就像“普通”矩阵一样)实际上是Eigen::Matrix模板类的实例。

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. 然而,为了简化程序员,Eigen使用C ++的typedef定义向量类,这些向量类是Eigen::Matrix<>同义词,带有特定选项。 For example, the Vector3d type in Eigen is a typedef for a matrix whose elements are double s and that has 3 rows and 1 column: 例如, EigenVector3d类型是一个矩阵的typedef ,该矩阵的元素为double ,并且具有3行和1列:

typedef Matrix<double, 3, 1> Vector3d

The Matrix template class actually has 6 template arguments, the last 3 ones being default arguments. Matrix模板类实际上有6个模板参数,最后3个是默认参数。 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: 如果编译器在错误消息中引用Eigen::Matrix<double, 3,1,0,3,1> ,则它是在谈论Eigen::Matrix并带有以下模板参数:

  • _Scalar = double _Scalar = double
  • _Rows = 3 _Rows = 3
  • _Cols = 1 _Cols = 1
  • _Options = 0 (by default) _Options = 0(默认情况下)
  • _MaxRows = _Rows (by default) = 3 _MaxRows = _Rows (默认情况下)= 3
  • _MaxCols = _Cols (by default) = 1 _MaxCols = _Cols (默认)= 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. 因此, Eigen::Matrix<double, 3,1,0,3,1>仅仅是丰满型的Vector3d编译器解决后看到typedef和模板参数。

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. 类型完全没有改变,您只需要在代码中使用Vector3d速记符号,而编译器则通过其显式类型对其进行引用。

If you are just interested in implementing Matrix and Vector using C++, then you can totally ignore my answer. 如果您只是对使用C ++实现Matrix和Vector感兴趣,则可以完全忽略我的回答。

But, if you just need to use Matrix and Vector, then you can try the Mat and Vec class in OpenCV . 但是,如果只需要使用 Matrix和Vector,则可以尝试OpenCV中MatVec类。 And here is a good tutorial about Mat. 这里是有关Mat的很好的教程。

In addition, if you don't necessarily need to use C++, then Octave is even more convenient. 另外,如果您不一定需要使用C ++,那么Octave更加方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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