How can I save a very large MATLAB sparse matrix to a text file?
Save the sparse matrix as a .mat
file. Then, in the other program, use a suitable library to read the .mat
file.
For instance, if the other program is written in Python, you can use the scipy.io.mio.loadmat
function, which supports sparse arrays and gives you a sparse numpy matrix.
You can use find to get index & value vectors:
[i,j,val] = find(data)
data_dump = [i,j,val]
You can recreate data from data_dump with spconvert, which is meant to "Import from sparse matrix external format" (so I guess it's a good export format):
data = spconvert( data_dump )
You can save to ascii with:
save -ascii data.txt data_dump
But this dumps indices as double, you can write it out more nicely with fopen/fprintf/fclose:
fid = fopen('data.txt','w')
fprintf( fid,'%d %d %f\n', transpose(data_dump) )
fclose(fid)
Hope this helps.