Search code examples
matlabmatrixsparse-matrix

how to get large scale sparse matrices in matlab, such as 10M*10M


How to get a sparse matrix of size 10M*10M in matlab which is normally distributed random matrix or uniformly distributed random matrix. When I do it below, I got error.

n = 10000000;   
a = sprandn(n,n,0.1);
fid = fopen('e:/matlab/matrix/normal_1M01.mtx','w');
len = nnz(a);
fprintf(fid,'%s\n','% by taoyuan');
fprintf(fid,'%d %d %d\n',n,n,len);
[m,k,s]=find(a);
for j = 1:len
    fprintf(fid,'%d %d %f\n',m(j),k(j),s(j));
end
fclose(fid);

The error is below:

??? Error using ==> rand
Maximum variable size allowed by the program is exceeded.

Error in ==> sprandn at 39
   i = fix( rand(nnzwanted, 1) * m ) + 1;

Error in ==> generate at 3
a = sprandn(n,n,0.1);

By the way,how to get a sparse matrix which is power-law distributed random matrix, the size is 10M*10M either.


Solution

  • The problem is that you are trying to generate nonzeros for one in every 10 elements.

    Try 10M * 10M * 0.1, and think about whether it is reasonable that you have hit the programs limits.

    If you have 0.1 as chance it is actually not very sparse. Either reduce the probability (a lot) or work in small batches.