Registered Member
|
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:
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. |
Registered Member
|
There is an example of both reading and writing a matrix from a binary file in this thread:
viewtopic.php?f=74&t=107161 |
Registered Member
|
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.
|
Moderator
|
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(); } |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]