Reputation: 84
Let's say I have a sparse matrix. I have it defined in the following CSV format:
row,column,value
1,1,5
1,2,10
In this case, the point (1,1) is equal to 5 and the point (1,2) is equal to 10.
What is an efficient way to create a matrix from this format (assuming thousands or hundreds of thousands of rows)?
In other words, I want the equivalent of running full(spconvert(m)) in Matlab, where m is the above matrix.
Upvotes: 1
Views: 384
Reputation: 35716
You need to use one of batch insertion constructors of sp_mat
sparse matrix class. There is an example of how to do it in documentation:
// batch insertion of two values at (5, 6) and (9, 9)
umat locations;
locations << 5 << 9 << endr
<< 6 << 9 << endr;
vec values;
values << 1.5 << 3.2 << endr;
sp_mat X(locations, values);
Upvotes: 2