Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert row vector to column vector in Eigen?

Tags:

c++

eigen

The documentation says:

... in Eigen, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; 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.

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?

like image 317
Martin Drozdik Avatar asked Feb 19 '13 10:02

Martin Drozdik


People also ask

How do I turn a row vector into a column vector?

Transpose. You can convert a row vector into a column vector (and vice versa) using the transpose operator ' (an apostrophe).

What is Eigen column vector?

... in Eigen, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; 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.

Is Eigen row or column major?

The default in Eigen is column-major. Naturally, most of the development and testing of the Eigen library is thus done with column-major matrices. This means that, even though we aim to support column-major and row-major storage orders transparently, the Eigen library may well work best with column-major matrices.

How do I resize an Eigen matrix?

Resizing. The current size of a matrix can be retrieved by rows(), cols() and size(). These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the resize() method.


1 Answers

The documentation for transposeInPlace says:

Note

if the matrix is not square, then *this must be a resizable matrix.

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.

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:

typedef Eigen::Matrix<double, Eigen::Dynamic, 1> ColumnVector;
ColumnVector column = row.transpose();
like image 107
Joseph Mansfield Avatar answered Oct 18 '22 16:10

Joseph Mansfield