问题 如何将犰狳矩阵转换为向量矢量?


我创建了一个armadillo c ++矩阵,如下所示:

arma::mat A; 
A.zeros(3,4);

我想将它转换为由定义的向量向量

std::vector< std::vector<double> > B(3, std::vector<double>(4) ); 

如何将B设置为A?如果对于矢量矢量没有简单的方法,那么阵列数组怎么样,即如果我将B定义为什么呢

double B[3][4]; 

4251
2018-03-16 23:35


起源



答案:


在这种情况下你应该使用 arma::conv_to 这是一个非常棒的arma功能。

请注意,此方法将要求源对象能够被解释为向量。这就是为什么我们需要为每一行迭代地执行此操作。这是一种转换方法:

#include <armadillo>

typedef std::vector<double> stdvec;
typedef std::vector< std::vector<double> > stdvecvec;

stdvecvec mat_to_std_vec(arma::mat &A) {
    stdvecvec V(A.n_rows);
    for (size_t i = 0; i < A.n_rows; ++i) {
        V[i] = arma::conv_to< stdvec >::from(A.row(i));
    };
    return V;
}

这是一个示例用法:

#include <iomanip>
#include <iostream>

int main(int argc, char **argv) {
    arma::mat A = arma::randu<arma::mat>(5, 5);
    std::cout << A << std::endl;

    stdvecvec V = mat_to_std_vec(A);
    for (size_t i = 0; i < V.size(); ++i) {
        for (size_t j = 0; j < V[i].size(); ++j) {
            std::cout << "   "
                << std::fixed << std::setprecision(4) << V[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

std::setprecision 用于生成更可读的输出:

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

祝你有个好的一天!


11
2018-03-26 17:52



为了避免不必要的复制,将函数声明从'mat_to_std_vec(arma :: mat A)'更改为'mat_to_std_vec(const arma :: mat&A)'。此上下文中的&字符表示引用A而不是复制A. - mtall
谢谢。这很棒。 - kchow462
@mtall:没错,这很重要。编辑了代码。 - ktalik


答案:


在这种情况下你应该使用 arma::conv_to 这是一个非常棒的arma功能。

请注意,此方法将要求源对象能够被解释为向量。这就是为什么我们需要为每一行迭代地执行此操作。这是一种转换方法:

#include <armadillo>

typedef std::vector<double> stdvec;
typedef std::vector< std::vector<double> > stdvecvec;

stdvecvec mat_to_std_vec(arma::mat &A) {
    stdvecvec V(A.n_rows);
    for (size_t i = 0; i < A.n_rows; ++i) {
        V[i] = arma::conv_to< stdvec >::from(A.row(i));
    };
    return V;
}

这是一个示例用法:

#include <iomanip>
#include <iostream>

int main(int argc, char **argv) {
    arma::mat A = arma::randu<arma::mat>(5, 5);
    std::cout << A << std::endl;

    stdvecvec V = mat_to_std_vec(A);
    for (size_t i = 0; i < V.size(); ++i) {
        for (size_t j = 0; j < V[i].size(); ++j) {
            std::cout << "   "
                << std::fixed << std::setprecision(4) << V[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

std::setprecision 用于生成更可读的输出:

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

祝你有个好的一天!


11
2018-03-26 17:52



为了避免不必要的复制,将函数声明从'mat_to_std_vec(arma :: mat A)'更改为'mat_to_std_vec(const arma :: mat&A)'。此上下文中的&字符表示引用A而不是复制A. - mtall
谢谢。这很棒。 - kchow462
@mtall:没错,这很重要。编辑了代码。 - ktalik