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

A templatized function to load dynamic size matrix from file

Tags: None
(comma "," separated)
linello
Registered Member
Posts
56
Karma
0
OS
Hi there, I was wondering if a simple templatized function can be written to load a matrix from a csv file where number are space-separated

something like this for example:
4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4
-1 5 0 -2 -3 1 0 -2 0 -3 -2 2 -1 -3 -2 -1 -1 -3 -2 -3 -1 0 -1 -4
-2 0 6 1 -3 0 0 0 1 -3 -3 0 -2 -3 -2 1 0 -4 -2 -3 3 0 -1 -4
-2 -2 1 6 -3 0 2 -1 -1 -3 -4 -1 -3 -3 -1 0 -1 -4 -3 -3 4 1 -1 -4
0 -3 -3 -3 9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4
-1 1 0 0 -3 5 2 -2 0 -3 -2 1 0 -3 -1 0 -1 -2 -1 -2 0 3 -1 -4
-1 0 0 2 -4 2 5 -2 0 -3 -3 1 -2 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
0 -2 0 -1 -3 -2 -2 6 -2 -4 -4 -2 -3 -3 -2 0 -2 -2 -3 -3 -1 -2 -1 -4
-2 0 1 -1 -3 0 0 -2 8 -3 -3 -1 -2 -1 -2 -1 -2 -2 2 -3 0 0 -1 -4
-1 -3 -3 -3 -1 -3 -3 -4 -3 4 2 -3 1 0 -3 -2 -1 -3 -1 3 -3 -3 -1 -4
-1 -2 -3 -4 -1 -2 -3 -4 -3 2 4 -2 2 0 -3 -2 -1 -2 -1 1 -4 -3 -1 -4

with unspecified number of rows and columns. They must be found probably with preliminary scan of the file and the matrix resized accordingly or is it possible to iteratively push the last row?
Do you have some suggestion about how to write it in the most useful manner? I think this is a very common thing and Eigen makes no mention of it.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
there are some examples in unsupported/Eigen/SparseExtra/MarketIO.h. In your case you can indeed first scan the file to get the sizes (you only need to scan the first line, and count the number of lines. Otherwise the best is to use a std::vector<MatrixType::Scalar>, push the value, and then use:

dest = Map<Matrix<MatrixType::Scalar,Dynamic,Dynamic,RowMajor> (&vec.at(0), rows, cols);


Bookmarks



Who is online

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