本征库->使用文件或现有std :: vector< string>中的数据初始化矩阵内容(C ++) [英] Eigen library --> initialize matrix with data from file or existing std::vector<string> content (c++)

查看:154
本文介绍了本征库->使用文件或现有std :: vector< string>中的数据初始化矩阵内容(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何初始化本征矩阵,但是这样:

My question is how to initialize an eigen Matrix, but NOT this way:

matrix << 1,0,1,0,
          1,0,1,0,
          1,0,1,0,

我有一个看起来像上面的矩阵(逗号或无逗号无关紧要) 存储在txt文件中.

I have a Matrix that looks like the above one ( commas or no commas doesnt matter) stored in a txt file.

我已经写了一个函数来读取每一行并将其放入向量中 现在我想用这些数据创建一个矩阵

I already wrote a function to read in each line and put it into a vector now I want to create a matrix with this data

但是它不起作用,我找不到任何页面解释如何在不只写值的情况下将数据分配给矩阵.(如上面的示例)

But it doesn' work and I cant find any page that explains how to assign data to a matrix without writing just the values.(like the example above)

我所需要的只是本征矩阵中文件中的数据

All I need is the data from my file in an eigen Matrix

到目前为止,我尝试过:(PS:有了迭代器的想法,但是我想对于非常大的矩阵,这将花费太长时间,我只是使用1-2维矩阵尝试了此示例)

What I tried so far: (PS: had the idea with the iterators but i guess it will take too long with really big matrices, I just tried this example with a 1-2 dimensional matrix)

int readFromFile (const char * path, vector <string> & mv)
{
    fstream file;
    string line;
    file.open(path);

    while (getline(file,line))
    {
        mv.push_back(line);
    }
    file.close();
    return 0;
}


typedef Matrix <int, 1, 2> MyMatrix;

int fromVectoEigen (vector<string> & source, MyMatrix & target)
{   //for (int i = source.size(); i<0 ; i--)
    //{

        string valuerow = source.back();
        string::iterator it = valuerow.begin();
        target.row(0)<< *it;
        target.row(0)<<*it+1;
        //source.pop_back();
    //}

    return 0;
}

很遗憾,不能只说不起作用的Matrix.row(i) = vector.back().

Unfortunately cant just say Matrix.row(i) = vector.back() that doesnt work.

推荐答案

以下代码适用于包含任意大小矩阵的文件:

The following code works with files containing matrices of arbitrary size:

#include <iostream>
#include <fstream>
#include <string>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

#define MAXBUFSIZE  ((int) 1e6)

MatrixXd readMatrix(const char *filename)
    {
    int cols = 0, rows = 0;
    double buff[MAXBUFSIZE];

    // Read numbers from file into buffer.
    ifstream infile;
    infile.open(filename);
    while (! infile.eof())
        {
        string line;
        getline(infile, line);

        int temp_cols = 0;
        stringstream stream(line);
        while(! stream.eof())
            stream >> buff[cols*rows+temp_cols++];

        if (temp_cols == 0)
            continue;

        if (cols == 0)
            cols = temp_cols;

        rows++;
        }

    infile.close();

    rows--;

    // Populate matrix with numbers.
    MatrixXd result(rows,cols);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            result(i,j) = buff[ cols*i+j ];

    return result;
    };

致谢.

这篇关于本征库->使用文件或现有std :: vector&lt; string&gt;中的数据初始化矩阵内容(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆