Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sparse Matrix Assignment becomes very slow in Matlab

I am filling a sparse matrix P (230k,290k) with values coming from a text file which I read line by line, here is the (simplified) code

while ...
            C = textscan(text_line,'%d','delimiter',',','EmptyValue', 0);
            line_number = line_number+1;
            P(line_number,:)=C{1};
end

the problem I have is that while at the beginning the

P(line_number,:)=C{1};

statement is fast, after a few thousands lines become exterely slow, I guess because Matlab need to find the memory space to allocate every time. Is there a way to pre-allocate memory with sparse matrixes? I don't think so but maybe I am missing something. Any other advise which can speed up the operation (e.g. having a lot of free RAM can make the difference?)

like image 429
Eugenio Avatar asked Dec 08 '25 10:12

Eugenio


1 Answers

There's a sixth input argument to sparse that tells the number of nonzero elements in the matrix. That's used by Matlab to preallocate:

S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros.

So you could initiallize with

P = sparse([],[],[],230e3,290e3,nzmax);

You can make a guess about the number of nonzeros (perhaps checking file size?) and use that as nzmax. If it turns you need more nonzero elements in the end, Matlab will preallocate on the fly (slowly).

like image 91
Luis Mendo Avatar answered Dec 10 '25 08:12

Luis Mendo