如何转换矢量的STL矢量到犰狳垫
How to convert STL vector of vector to armadillo mat?
给定vector<vector<double > > A_STL
,我想把它转换成arma::mat A
一种简单的方法是将向量矩阵的向量平坦化为一维向量。因此,您可以使用mat(std::vector)
构造函数。
// Flatten your A_STL into A_flat
std::vector<double> A_flat;
for (auto vec : A_STL) {
for (auto el : vec) {
A_flat.push_back(el);
}
}
// Create your Armadillo matrix A
mat A(A_flat);
注意向量的向量排序。
下面几行应该可以完成这项工作:
template<typename T>
arma::Mat<T> convertNestedStdVectorToArmadilloMatrix(const std::vector<std::vector<T>> &V) {
arma::Mat<T> A(V.size(), V[0].size());
for (unsigned_t i{}; i < V.size(); ++i) {
A.row(i) = arma::conv_to< arma::Row<T> >::from(V[i]);
}
return A;
}
测试:std::vector<std::vector<float_type>> vec = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}};
arma::Mat<float_type> mat = convertNestedStdVectorToArmadilloMatrix(vec);
相关文章:
- 如何使用 STL 算法将整数向量转换为字符串向量?
- stl::unordered_map 和 stl::vector 的销毁有何不同
- 如何在<typename> <long> 不更改给定代码的情况下将自定义向量与 STL 向量相互转换?
- 两种类型转换有何不同?
- 将 STL 模型转换为 OSG 格式
- C 中STL结构的类型转换
- STL标准::转换
- C++ STL 包含器到 Glib 转换
- 如何使用STL将结构的矢量转换为映射
- 如何将RAW文件(3D图像)转换为STL格式
- 将STL列表转换为二进制搜索树递归模板
- 将 STL unicode 字符串转换为 wxString 会给出空字符串
- 将 STL 映射转换为结构
- 如何将列表 STL 的迭代器转换为实例 (C++)
- 在 STL 堆栈和双端格式之间进行转换
- gdb:将内存地址强制转换为 STL 对象
- c++,stl.将具有rgb颜色的字符串转换为3个int值
- STL 和 Hana 元组之间的转换
- 将 STL 容器<T *> 转换为容器<T 常量 *>
- 如何从"STL"格式转换为"3DS"