MATLAB Answers

what is the optimal way to build a sparse matrix from row/col index, and value data?

13 ビュー (過去 30 日間)
Brian
Brian 2012 年 7 月 22 日
Hello,
To objectively frame the reason I'm asking this question, I should say that I am exploring some very large datasets that will be pushing the limits of my pc, at least I expect... So I want to find the solution to my question that provides the most efficient way to construct a sparse matrix from a matrix containing the row and column index, and contents values.
My data: From a mysql database, I am executing a fetch query which returns to matlab a Nx3 matrix, where there are N rows depending on the query.
What I need to do: I need to efficiently construct a sparse matrix from this data.
The values in the first column translate to the a unique row in the output sparse matrix. The values in the second column correspond to a unique column in the output sparse matrix. And values in the third column, represent the value to be placed at the corresponding cell withing the output sparse matrix.
So, in my Nx3 matrix there is not necessarily a value for every row and column, and thus I cannot use a simple reshape operation.
Is there a vectorized way to reconstruct the sparse matrix from row/column index and content data, the way that I've described?
Ideally the method of doing this might have matlab's code optimizer use a series of pointers. As I'm dealing with large datasets, I don't think my system will have adequate overhead resources to deal with any memory copy operations...
Any suggestions would be appreciated!
Thanks, Brian

  0 件のコメント

サインインしてコメントする。

採用された回答

Sargondjani
Sargondjani 2012 年 7 月 22 日
did you check 'sparse'? i think this will do what you want:
S = sparse(i,j,s) where i,j,s are your 3 column vectors...

  1 件のコメント

Brian
Brian 2012 年 7 月 23 日
Thanks, Sargondjani. That's exactly what I need.

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2012 年 7 月 22 日
Is the result of the query sorted? I.e., is it already in column major order the way MATLAB will naturally store it? (1,1) followed by (2,1) followed by (3,1) etc. Also, can you do the query so that you get three separate Nx1 matrices instead of a single Nx3 matrix? If all of that is true, then a mex routine could convert these vectors into a sparse matrix directly without copying the data at all (the sparse function will copy it).

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by