简体   繁体   English

如何将 std::vector 转换为 Eigen 中的矩阵?

[英]How to convert an std::vector to a matrix in Eigen?

I am somewhat new to Stack Overflow and C++ so feel free to correct any errors in my code and the formatting of this question.我对 Stack Overflow 和 C++ 有点陌生,所以请随时纠正我的代码中的任何错误和这个问题的格式。

I am trying to make a linear regression calculator using the normal equation which involved the transposing of matrices and multiplication of vectors (and their inverses).我正在尝试使用涉及矩阵转置和向量(及其逆)乘法的正规方程制作线性回归计算器。 The program is supposed to read from a csv file and pass the information from that file into a matrix and calculate the regression line.该程序应该从 csv 文件中读取并将该文件中的信息传递到矩阵中并计算回归线。 To make the job easier, I decided to use a library called Eigen for matrix-matrix multiplication.为了简化工作,我决定使用一个名为 Eigen 的库来进行矩阵-矩阵乘法。

The problem that I have run into is that the Map function can only take in an array as opposed to a std::vector.我遇到的问题是Map function 只能接受数组而不是 std::vector。

This is what I have so far:这是我到目前为止所拥有的:

float feature_data[] = { 1, 1, 1, 1, 1, 1,
                         2, 4.5, 3, 1,4, 5};
float labels[] = { 1, 4, 3, 2, 5, 7 };


//maps the array to a matrix called "feature_data"
MatrixXf mFeatures = Map< Matrix<float, 6, 2> >(feature_data);
MatrixXf mLabels = Map< Matrix<float, 6, 1> >(labels);

//use the toArray function
std::vector<float> test_vector = { 2,1,3 };
float* test_array = toArray(test_vector);


calcLinReg(mFeatures, mLabels);

const int n = 2;
int arr[n];

system("pause");

For context, the toArray function is my unsuccessful attempt to make an array from a vector (in all honesty, it works but it returns a pointer which you can't pass into the Map function in Eigen.) calcLinReg does exactly what it sounds like: calculates the linear regression line parameters.对于上下文,toArray function 是我从向量中创建数组的不成功尝试(老实说,它可以工作,但它返回一个指针,您不能将其传递到 Eigen 中的Map function。) calcLinReg确实像它听起来的那样:计算线性回归线参数。

Is there anyway I can convert a vector to an array or convert a vector to a matrix in Eigen?无论如何我可以将向量转换为数组或将向量转换为 Eigen 中的矩阵?

How about trying to use the vectors data() method , which gives you access to the memory array used internally by the vector, like this: 如何尝试使用vectors data()方法 ,该方法使您可以访问vector内部使用的内存数组,如下所示:

    std::vector<float> test_vector = { 2,1,3 };
    float* test_array = test_vector.data();
    Eigen::MatrixXf test = Eigen::Map<Eigen::Matrix<float, 3, 1> >(test_array);

Or shorter: 或更短:

    std::vector<float> test_vector = { 2,1,3 };
    Eigen::MatrixXf test = Eigen::Map<Eigen::Matrix<float, 3, 1> >(test_vector.data());

Beware The asignment actually copies the data, therefore this is safe. 当心指点实际上是复制数据,因此这是安全的。 However, you can also directly use the data of the vector like this 但是,您也可以像这样直接使用向量的数据

    std::vector<float> test_vector(3,2);
    Eigen::Map<Eigen::Matrix<float, 3, 1> > dangerousVec (test_vector.data());

If vector goes out of scope the memory is deallocated and dangerousVec's data is dangeling. 如果矢量超出范围,则内存将被释放,并且angeredVec的数据将处于混乱状态。

Someone in a comment is asking for the case of dynamic numbers of rows and columns.评论中有人询问动态行数和列数的情况。 This is possible, as follows:这是可能的,如下所示:

typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> MyMatrix;

size_t nrow = ...;
size_t ncol = ...;
MyMatrix M = Eigen::Map<MyMatrix>(test_vector.data(), nrow, ncol);

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

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