This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Inputting data straight into an Eigen matrix.

Tags: None
(comma "," separated)
brianroseman
Registered Member
Posts
2
Karma
0
Hi friends,

I've searched this forum, as well as many C++ forums for an answer to my question but have not found any.

What I'm trying to do is write an econometric program that can handle large data sets. I have part of the code working, but the problem I am struggling with (and the code that isn't working) is how to input a dynamic size matrix into C++ so I can play around with it doing matrix multiplication. I've tried a few things with trying to input the data into an array, and then having an eigen vector or matrix read it. (this hasn't worked so far)

Let me show you my code so far:
Code: Select all
MatrixXd readPortfolio()  //this will be a function that should hopefully return a matrix to use in other functions.
   {
   MatrixXd matrix(n,k);
   RowVectorXd vector(1,k);
   ifstream data("data.bin");
   string lineOfData;

   if (data.is_open())
   {
   int i=0;
      while (data.good())
      {
      getline (data,lineOfData);
      cout<<lineOfData<<endl; //just to check if the data was read.
      matrix.row(i) = lineOfData;
      i++;
      }
   }
   else cout<<"Unable to open file";
   return matrix;
   }


Any help will be great. My c++ experience is very limited, I have only used it in academic settings to if its possible please describe as many details as possible, and any responses are hopefully written simply.
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS
There is an example of both reading and writing a matrix from a binary file in this thread:
viewtopic.php?f=74&t=107161
brianroseman
Registered Member
Posts
2
Karma
0
I'm getting an error when using that code:

ReadData.cpp: In function ‘void load(const char*)’:
ReadData.cpp:38:2: error: ‘m_isAllocated’ was not declared in this scope
ReadData.cpp:39:18: error: request for member ‘bin’ in ‘data’, which is of non-class type ‘const char*’
ReadData.cpp:40:18: error: ‘m_rows’ was not declared in this scope
......
.. and it continues for about a dozen lines of similar errors

I assume it is because I am missing a file that I didn't include. This is what I have included at the top of my page.
Code: Select all
#include <iostream>
#include <Eigen/Dense>
#include <cmath>
#include <boost/random.hpp>
#include <boost/math/distributions.hpp>
#include <fstream>
using namespace Eigen;
using namespace std;
using namespace boost;
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
this code was just for inspiration, you need to adjust it to first save/read the size of the matrix and then the data:

template<typename MatrixType>
void save(const char *filename, const MatrixType& m) const
{
ofstream f(filename, ios::binary);
f.write((char *)&m.rows(), sizeof(m.rows()));
f.write((char *)&m.cols(), sizeof(m.cols()));
f.write((char *)&m.data(), sizeof(typename MatrixType::Scalar)*m.cols()*m.cols());
f.close();
}

template<typename MatrixType>
void load(const char *filename, MatrixType& m)
{
typename MatrixType::Index rows, cols;
ifstream f(filename, ios::binary);
f.read((char *)&rows, sizeof(rows));
f.read((char *)&cols, sizeof(cols));
m.resize(rows, cols);
f.read((char *)&m.data(), sizeof(typename MatrixType::Scalar)*rows*cols);
if (f.bad())
throw std::exception("Error reading matrix");
f.close();
}


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]