E-Dreamer

脚踏实地,仰望星空

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.matrixXd 与 vector<double> 的互转

vector<double> 转 matrixXd

Eigen::MatrixXd Vec_xx = Eigen::Map<Eigen::MatrixXd>(new_x.data(), new_x.size(),1);

vector<double> 转 eigen::Vector

Eigen::VectorXd pf = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(arg.data(), arg.size());

MatrixXd 转 vector<double>

vector<double> vec_norm(sqrt_norm.data(),sqrt_norm.data()+sqrt_norm.rows()*sqrt_norm.cols());

vector 的相减

std::vector <double> x0(X.begin(),X.end()-1); 
std::vector <double> x1(X.begin()+1,X.end());
std::vector <double> dx;
std::transform(x0.begin(), x0.end(), x1.begin(),std::back_inserter(dx),std::minus<double>());

csv 读写

写
void Spline_process::csv_save(Eigen::MatrixXd &res){
    ofstream dataFile;
    dataFile.open("sp.csv", ios::out | ios::trunc);
    for (int i = 0; i < res.rows(); i++)
    {
        for (int j = 0; j < res.cols(); j++)
        {
            dataFile << res(i,j) << ",";          // 写入数据
            // cout << res(i,j) << ","; 
        }
        dataFile <<  endl;                       // 换行
        // cout << endl; 
    }
    
    dataFile.close();                            // 关闭文档
}
读
std::ifstream f;
f.open("install/app_controller/lib/app_controller/test.csv");   /* open file with filename as argument */
if (!f.is_open()) {    /* validate file open for reading */
    std::cerr << "error: file open failed " << ".\n";
    return ;
}
std::string line, val;                  /* string for line & value */
std::vector<std::vector<double>> array;    /* vector of vector<int>  */

while (std::getline (f, line)) {        /* read each line */
    std::vector<double> vv;                 /* row vector v */
    std::stringstream s (line);         /* stringstream line */
    while (getline (s, val, ','))       /* get each value (',' delimited) */
        vv.push_back (std::stod (val));  /* add to row vector */
    array.push_back (vv);                /* add row vector to array */
}  
posted on 2023-04-19 10:12  E-Dreamer  阅读(263)  评论(0编辑  收藏  举报