I have a huge matrix(584064x5369468) and I have to use sparse matrix in Matlab, but in my calculation it is very slow to assign a value to a sparse matrix. I cannot wait to finish it, what should I do.
cgm=sparse(584064,5369468);
[Xcp, Ycp, Zcp] = ndgrid(-[1:336],-[1:45],-[1:336]);
% dp my data which is loaded
for sg=1:335
for tg=1:44
for rg=1:335
% ii is a vector i.e the size of ii = 309x1
fprintf('Constructing patch: %i %i %i',sg,tg,rg)
ii=find(abs((Xcp(sg,tg,rg))<=abs(dp(:,1)))&abs((dp(:,1))<abs(Xcp(sg+1,tg,rg)))&(abs(Ycp(sg,tg,rg))<=abs(dp(:,2)))& (abs(dp(:,2))<abs(Ycp(sg,tg+1,rg)))& (abs(Zcp(sg,tg,rg))<=abs(dp(:,3)))&(abs(dp(:,3))<abs(Zcp(sg,tg,rg+1))));
% jj is another vector i.e the size of jj = 64*1
%the ii size is changing every time but the size of jj is always 64. ii is non-zero values and can be between 1 and 584064 .
% c is my matrix after calculation i.e c = 309x64
cgm(ii,jj)=c;
end
end
end
For the first internal loop it goes very fast but it become slower and slower drastically.
I know that assigning value to sparse matrix means repack everything and it is very slow, but how to repack it less in my code?
Thanks
ii
andjj
being different sizes. – beakerS = sparse(i, j, v)
as shown here (or maybeS = sparse(i, j, v, m, n)
). – beakerndgrid
were not there before. Either way, thanks. – rayryengi
,j
, andv
are allN x 1
vectors and populate your sparse matrixC
such thatC(i(k), j(k)) = v(k)
fork = 1, 2, ... , N
. All other values would be zero. I would also recommend you override the size and doC = sparse(i, j, v, 584064,5369468);
so that you're sure you get the desired size. – rayryeng