How can I save a very large MATLAB sparse matrix to a text file?

前端 未结 8 717
孤城傲影
孤城傲影 2021-02-01 06:53

I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to use in another program. Calling save won\'t write this as ASCII (not supported). Calling full()

8条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 07:36

    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.

提交回复
热议问题