Last active
May 4, 2016 04:47
-
-
Save Shusei-E/f632c9a7b7e197cf50709915d210f7c8 to your computer and use it in GitHub Desktop.
Eigen Read Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original Code http://goo.gl/P0sED6 | |
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modified code of http://goo.gl/P0sED6 | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <Eigen/Dense> | |
using namespace std; | |
using namespace Eigen; | |
#define MAXBUFSIZE ((int) 1e6) | |
VectorXd readVector(const char *filename){ // http://goo.gl/P0sED6 | |
int length=0; | |
double buff[MAXBUFSIZE]; | |
// Read numbers into buffer | |
ifstream infile; | |
infile.open(filename); | |
int temp_cols=0; | |
while (! infile.eof()){ | |
string line; | |
getline(infile, line); | |
stringstream stream(line); | |
stream >> buff[temp_cols++]; | |
if(temp_cols==0) | |
continue; | |
length++; // keep vector length | |
} // close while ! infile.eof() | |
infile.close(); | |
//rows--; | |
// return vector | |
VectorXd result(length); | |
for(int i=0; i<length; i++){ | |
result[i] = buff[i]; | |
} //close for(i) | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment