Eigen library --> initialize matrix with data from file or existing std::vector content (c++)

前端 未结 7 1119
你的背包
你的背包 2020-12-25 08:00

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,
         


        
7条回答
  •  一生所求
    2020-12-25 08:51

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

    #include 
    #include 
    #include 
    #include 
    
    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;
        };
    

    Regards.

提交回复
热议问题