有许多链接可以反过来转换,但我无法找到在我的特定情况下从Eigen::Matrix或Eigen::VectorXd获取std::vector的方法。
vector<int> vec(mat.data(), mat.data() + mat.rows() * mat.cols());
mat.rows() * mat.cols()
可以简化为 mat.size()
,但要注意这种解决方案仅适用于普通的 Matrix<>
对象,如果使用我的答案中所示的 Map<>
,则可适用于子矩阵。 - ggaelvector<double> vec(arr.cols()); Map<RowVectorXd>(&vec[0], 1, mat.cols()) = mat.row(0);
- zardosht你不能使用类型转换,但你可以轻松地复制数据:
VectorXd v1;
v1 = ...;
vector<double> v2;
v2.resize(v1.size());
VectorXd::Map(&v2[0], v1.size()) = v1;
//init a first vector
std::vector<float> v1;
v1.push_back(0.5);
v1.push_back(1.5);
v1.push_back(2.5);
v1.push_back(3.5);
//from v1 to an eignen vector
float* ptr_data = &v1[0];
Eigen::VectorXf v2 = Eigen::Map<Eigen::VectorXf, Eigen::Unaligned>(v1.data(), v1.size());
//from the eigen vector to the std vector
std::vector<float> v3(&v2[0], v2.data()+v2.cols()*v2.rows());
//to check
for(int i = 0; i < v1.size() ; i++){
std::cout << std::to_string(v1[i]) << " | " << std::to_string(v2[i]) << " | " << std::to_string(v3[i]) << std::endl;
}
v1.data()
应该被替换为 ptr_data
,对吗? - quant_dev