[英]How to map/built the c++ vector from the eigen matrix?
MatrixXf A = MatrixXf::Random(3, 3);
MatrixXf B = A.row(1);
std::vector<float> vec;
I want to built the vector "vec" with elements from the row Eigen matrix "B". 我想用行特征矩阵“ B”中的元素构建向量“ vec”。 Something like this "vec=B.data()"
像这样的“ vec = B.data()”
In addition to the obvious answer (manual push_back
s or pre-allocation + index-by-index assignment), you can initialize it directly using the base pointer returned by ::data()
: 除了显而易见的答案(手动
push_back
或预分配+逐索引分配)之外,您还可以使用::data()
返回的基本指针直接将其初始化:
Eigen::MatrixXf A = Eigen::MatrixXf::Random(3, 3);
Eigen::MatrixXf B = A.row(1);
std::vector<float> vec(B.data(), B.data() + B.size());
Just be careful that Eigen may use memory alignment to take advantage of SSE-family instructions, so it may not work correctly with higher dimensions. 请注意,Eigen可能会使用内存对齐方式来利用SSE系列指令,因此在尺寸较大时可能无法正常工作。
Just use a loop: 只需使用一个循环:
MatrixXf A = MatrixXf::Random(3, 3);
MatrixXf B = A.row(1);
std::vector<float> vec(B.size());
for(size_t row=0; row < vec.size(); row++) {
vec[row] = B[row];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.