Shai commented here
"BTW, have you tried storing the mlf as a column vector rather than a row vector? sparse([],[],[],2^31, 1, 500);? If I'm not mistaken this should be easier to handle with Matlab's internal representation of sparse matrices."
and that did it!
>> tic;sparse([],[],[],2^31,1);toc
Elapsed time is 0.549435 seconds.
>> tic;sparse([],[],[],1,2^31);toc
Elapsed time is 15.102854 seconds.
Amazing!
Why is that so?
(If I'm allowed to so bluntly edit a post) Matlab's sparse matrices are stroed using three vectors: one that stores the row index of the non-zeros entries. The second stores the column index but in a compressed manner. Finally, the third vector stores the actual value of each entry.
The first and last vectors always have length as the number of non-zeros elements in the matrix. However, the compressed second one has length as the number of columns of the matrix regardless of the number of non-zero elements in the matrix.
Therefore, transposing mlf
from 2^31
columns to 1 has a huge impact on the size of the second vector - and this is why the timing is so drastically affected.