C++ 将arma::cx_mat转换为数组数组

C++ 将arma::cx_mat转换为数组数组,c++,c++11,armadillo,C++,C++11,Armadillo,如何将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

如何将
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;
}
std::向量矩阵到向量向量(arma::cx矩阵和矩阵)
{
标准:向量vv(M.n_行);

对于(size_t i=0;i如果需要将实际零件从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;
}
double**mat\u to\u carr(arma::cx\u mat&M,std::size\u t&n,std::size\u t&M)
{
const std::size\t nrows=M.n\u行;
常数std::size\u t ncols=M.n\u cols;
double**数组=(double**)malloc(nrows*sizeof(double*);
对于(std::size\u t i=0;i
注意,当不再需要阵列时,需要释放阵列。 例如:

intmain()
{
cx_材料X(5,5,填充::randn);
标准:尺寸n,m;
自动阵列=mat_to_carr(X,n,m);
对于(标准::大小\u t i=0;istd::cout如果需要将cx\u 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;
}
double**mat\u to\u carr(arma::cx\u mat&M,std::size\u t&n,std::size\u t&M)
{
const std::size\t nrows=M.n\u行;
常数std::size\u t ncols=M.n\u cols;
double**数组=(double**)malloc(nrows*sizeof(double*);
对于(std::size\u t i=0;i
注意,当不再需要阵列时,需要释放阵列。 例如:

intmain()
{
cx_材料X(5,5,填充::randn);
标准:尺寸n,m;
自动阵列=mat_to_carr(X,n,m);
对于(标准::大小\u t i=0;istd::coutcx_mat是复数矩阵,那么你想要什么类型的C-数组?@Atomic_警报好问题,我想保存平方范数。这应该是实数,对吧?如果不是,那么我只想保存实数部分。对于这个问题,你可以假设我只想在双数组中保存矩阵的实数部分是的。但是如果你需要得到平方范数,那么为什么需要转换成数组呢?cx_mat是复杂的矩阵,那么你想要得到什么类型的C-数组?@Atomic_alarm好问题,我想保存平方范数。这应该是一个实数,对吗?如果不是,那么我只想保存实数部分。对于这个问题,你可以假设我想要在双数组数组中只保存矩阵的实部。是的。但是如果需要得到平方范数,为什么需要转换为数组?