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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by