Increased time for setting elements in sparse matrix

2 views (last 30 days)
Hi Everyone,
I am trying to create a large sparse matrix. After creating the matrix I set elements by creating blkdiag matrices in a for loop.
However, each iteration of the for loop takes longer than the previous one.
Is there a more efficient way to set the elements?
I created a small code fragment to highlight the issue:
n=80000;
m=80000;
p=500;
H=sparse(n,m);
time_vec=zeros(n/p,1);
for i=1:n/p
t1=tic;
dat_cells=repmat({1:n/p},1,p);
H((i-1)*p+1:(i)*p,:)=sparse(blkdiag(dat_cells{:}));
time_vec(i)=toc(t1);
end
figure,plot(time_vec)
  1 Comment
Markus Adamek
Markus Adamek on 6 Jul 2022
Updated solution based on the answer below:
This solution takes ~3seconds, can be further vectorized but gets across the idea:
n=800;
m=800;
p=50;
H=sparse(n,m);
time_vec=zeros(n/p,1);
i=[];
j=[];
v=[];
t1=tic;
for mm=1:m/p
for ii=1:n/p
for jj=1:p
i(end+1)=(ii-1)*length(p)+1+(mm-1)*m/p;
j(end+1)=ones(length(p),1)*(jj+p*(ii-1));
v(end+1)=ii*ones(length(p),1);
end
end
end
toc(t1)
H=sparse(i,j,v,n,m);

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 6 Jul 2022
Every time you change the elements of a sparse matrix, MATLAB has to deep copy all the existing elements to a newly allocated chunk of memory with enough room for the current elements and your new elements. If you do this repeatedly, the same elements get deep copied over and over again. This is the drag on your performance. If possible in your application, it is better to generate all the new elements off to the side and then add them into the sparse matrix all at once instead of piecemeal.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!