How to quickly accumulate values into sparse matrix from very large row,col and value arrays containing duplicates indices?

5 views (last 30 days)
Hi,
I'd like to fill a large (sparse) matrix. I have three very long arrays of rowidx, colidx and values. row col combination can appear several times in those arrays and I'd like to add the corresponding values into the matrix at that location
% alloc sparse zero matrix
A = spalloc(vnum,vnum,vnum*maxdeg);
% I and J are row and col idxs
% val is the vector of values (all same length)
for i = 1:length(I)
A(I(i),J(i)) = A(I(i),J(i)) + val(i);
end
Takes forever. Is there a faster way? I tried also this
Idx = I+vnum*(J-1);
v = accumarray(Idx,val);
to sum up the duplicates first, but that gives me out of memory (maybe it creates a dense large matrix internally?).
Any help appreciated

Accepted Answer

Martin
Martin on 31 Oct 2012
Just found the answer:
A=sparse(I,J,val);
does the trick (it actually accumulates at duplicated index values).

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!