Increased time for setting elements in sparse matrix

조회 수: 3 (최근 30일)
Markus Adamek
Markus Adamek 2022년 7월 6일
댓글: Markus Adamek 2022년 7월 6일
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
Markus Adamek
Markus Adamek 2022년 7월 6일
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);

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2022년 7월 6일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by