简体   繁体   English

如何在Eigen中将行向量转换为列向量?

[英]How to convert row vector to column vector in Eigen?

The documentation says: 文件说:

... in Eigen, vectors are just a special case of matrices, with either 1 row or 1 column. ...在Eigen中,向量只是矩阵的一个特例,有1行或1列。 The case where they have 1 column is the most common; 他们有1列的情况是最常见的; such vectors are called column-vectors, often abbreviated as just vectors. 这种矢量称为列矢量,通常缩写为矢量。 In the other case where they have 1 row, they are called row-vectors. 在他们有1行的另一种情况下,它们被称为行向量。

However this program outputs unintuitive results: 但是,该程序输出的结果不直观:

#include <eigen3/Eigen/Dense>
#include <iostream>

typedef Eigen::Matrix<double, 1, Eigen::Dynamic> RowVector;

int main(int argc, char** argv)
{
    RowVector row(10);
    std::cout << "Rows: "    << row.rows() << std::endl;
    std::cout << "Columns: " << row.cols() << std::endl;
    row.transposeInPlace();
    std::cout << "Rows: "    << row.rows() << std::endl;
    std::cout << "Columns: " << row.cols() << std::endl;
}

Output: 输出:

Rows: 1
Columns: 10
Rows: 1
Columns: 10

Is this a bug, or am I using the library incorrectly? 这是一个错误,还是我错误地使用了库?

The documentation for transposeInPlace says: transposeInPlace的文档说:

Note 注意

if the matrix is not square, then *this must be a resizable matrix. 如果矩阵不是正方形,则*this必须是可调整大小的矩阵。

You'll need your type to have both dynamic rows and columns: 您需要您的类型同时拥有动态行和列:

Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>

However, there's already a typedef for this: MatrixXd . 但是,已有一个typedefMatrixXd

Alternatively, if you still want the compile-time sizes, you can use tranpose rather than transposeInPlace to give you a new transposed matrix rather than modify the current one: 或者,如果您仍然需要编译时大小,则可以使用tranpose而不是transposeInPlace为您提供新的转置矩阵,而不是修改当前的矩阵:

typedef Eigen::Matrix<double, Eigen::Dynamic, 1> ColumnVector;
ColumnVector column = row.transpose();

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

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