本征将矩阵转换为矢量 [英] Eigen Convert Matrix to Vector
本文介绍了本征将矩阵转换为矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 MATLAB 中,下面的行将Matrix转换为Vector.它将矩阵逐列展平为vector.
In MATLAB, the line below converts a Matrix to a Vector.It flattens the matrix column by column into a vector.
myvar(:)
我应该如何使用Eigen来做到这一点?该解决方案应适用于任何尺寸的Matrix.
How do I do that with Eigen? The solution should work for any dimension of Matrix.
MatrixXd A(3,2);
VectorXd B(6);
A << 1,2,3,4,5,6;
B << A.col(0), A.col(1);
//This isn't general enough to work on any size Matrix
推荐答案
本征矩阵默认情况下按列主顺序存储,因此您可以使用本征映射将数据逐列存储在数组中:
Eigen matrices are stored in column major order by default, so you can use simply use Eigen Maps to store the data column by column in an array:
MatrixXd A(3,2);
A << 1,2,3,4,5,6;
VectorXd B(Map<VectorXd>(A.data(), A.cols()*A.rows()));
如果要按行对数据进行排序,则需要先对矩阵进行转置:
If you want the data ordered row by row, you need to transpose the matrix first:
MatrixXd A(3,2);
A << 1,2,3,4,5,6;
A.transposeInPlace();
VectorXd B(Map<VectorXd>(A.data(), A.cols()*A.rows()));
这篇关于本征将矩阵转换为矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文