Time of sparse matrix components allocation

1 次查看(过去 30 天)
Nobs = 20000;
K = 20;
tic
H = sparse([],[],[],Nobs,Nobs,4*K*Nobs);
for j = 1 : Nobs
jj = randi(Nobs,1,K);
H(j,jj) = 1;
H(jj,j) = 1;
end
toc
Hi,
I have a problem with sparse matrix components allocation. Why does the allocation of matrix components slow down as the loop moves forward (increase of j). Run time of the above code with n = 20000 is 6 s., but with n = 60000 (tripled), run time becomes 90s. (15 times greater). How can i fix this problems?
With spacial thanks

采纳的回答

David Goodmanson
David Goodmanson 2020-5-2
编辑:David Goodmanson 2020-5-2
Hi reza,
with sparse matrices you want to build up a set of indices to use in the first two inputs, in order to make a single call to sparse if possible. That process is pretty fast. Updating a large sparse matrix is much, much slower.
Nobs = 20000;
K = 20;
tic
ind1 = repmat(1:Nobs,K,1);
ind1 = ind1(:); % indices 1 to Nobs, each repeated K times
ind2 = randi(Nobs,Nobs*K,1); % full set of random indices
ind12 = [ind1 ind2];
ind = [ind12; fliplr(ind12)]; % fliplr produces the transposed element
H1 = sparse(ind(:,1),ind(:,2),1);
toc
Elapsed time is 0.120881 seconds.

更多回答(1 个)

James Tursa
James Tursa 2020-5-2
Every time you add even one element to a sparse matrix, it has to copy data ... perhaps all of the current data ... to make room for your new element. And if the current allocated sizes aren't enough, it has to allocate new memory as well. The bulk of your timing (often 99% of it) is being spent in repeated data copying (many, many times for the same elements) instead of the actual element assignment.
With sparse matrices, you should gather all of the indexing and data values up front, and build it only once to avoid this data copying and extra allocations.

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by