将arma::cx_mat转换为数组数组
Convert arma::cx_mat to array of arrays
如何将arma::cx_mat
转换为数组?
转换的动机是使用libmatio
(一个C库)来输出.mat文件。
到目前为止,我已经创建了一个函数来将arma:cx_mat
转换为矢量的矢量:
std::vector<std::vector<double>> mat_to_vv(arma::cx_mat &M)
{
std::vector<std::vector<double>> vv(M.n_rows);
for(size_t i=0; i<M.n_rows; ++i)
{
vv[i] = arma::conv_to<std::vector<double>>::from(M.row(i));
};
return vv;
}
如果您需要将实际部分从cx_mat转换为数组的C数组,可以使用此函数:
double** mat_to_carr(arma::cx_mat &M,std::size_t &n,std::size_t &m)
{
const std::size_t nrows = M.n_rows;
const std::size_t ncols = M.n_cols;
double **array = (double**)malloc(nrows * sizeof(double *));
for(std::size_t i = 0; i < nrows; i++)
{
array[i] = (double*)malloc(ncols * sizeof(double));
for (std::size_t j = 0; j < ncols; ++j)
array[i][j] = M(i + j*ncols).real();
}
n = nrows;
m = ncols;
return array;
}
请注意,不再需要时需要释放数组。示例:
int main()
{
cx_mat X(5, 5, fill::randn);
std::size_t n,m;
auto array = mat_to_carr(X,n,m);
for (std::size_t i = 0; i < n; ++i)
{
for (std::size_t j = 0; j < m; ++j)
std::cout<<array[i][j]<<" ";
std::cout<<std::endl;
}
for(std::size_t i = 0; i < n; i++)
free(array[i]);
free(array);
return 0;
}
相关文章:
- 如何在C++中将数组转换为矢量指针
- 将C++中的数组转换为字符串
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 将 int 数组转换为 std::vector<int*>
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将字符数组转换为时间结构
- 将字节数组转换为多维数组
- 可视化 如何在C++中将字符数组转换为 FILE 类型
- 在 C++ 中将整数数组转换为位集表示形式的最佳方法?
- C++如何将字符数组转换为字节向量(向量<byte>)
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 无法将字符数组转换为包含 utf-8 字符的字符串
- 如何使用 C++将 2D RGB 数组转换为 dds 文件?
- 如何将字符串指针数组转换为字符串类型的智能指针向量?
- 将 int 数组转换为带有小数C++的双精度数组
- 使用 .data() 将字符数组转换为 std::string 不会转换整个数组
- 将字符数组转换为整数值