我在 MATLAB(版本 7)中有一个 30000x14000 稀疏矩阵,我需要在另一个程序中使用它。调用 save 不会将其写为 ASCII(不支持)。调用full()
这个怪物会导致 Out of Memory
错误。
我如何导出它?
8 回答
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.
将稀疏矩阵保存为.mat
文件。然后,在另一个程序中,使用合适的库来读取.mat
文件。
例如,如果另一个程序是用 Python 编写的,您可以使用该scipy.io.mio.loadmat
函数,该函数支持稀疏数组并为您提供稀疏的 numpy 矩阵。
我在 MATLAB 中使用 Java 将其保存为文本。MATLAB 代码:
pw=java.io.PrintWriter(java.io.FileWriter('c:\\retail.txt'));
line=num2str(0:size(data,2)-1);
pw.println(line);
for index=1:length(data)
disp(index);
line=num2str(full(data(index,:)));
pw.println(line);
end
pw.flush();
pw.close();
这data
是一个非常大的稀疏矩阵。
你试过分区吗?
我的意思是尝试在前 1000 行(或 5000 行)上调用 full(),如果可行,则重复该过程。
使用该find
函数获取非零元素的索引...
idcs = find(data);
vals = data(idcs);
...save the index vector and value vector in whatever format you want...
If you want, you can use ind2sub
to convert the linear indices to row, column subscripts.
If you need to recreate a sparse matrix in matlab from subscripts + values, use spconvert
.
dlmwrite - Write matrix to ASCII-delimited file Syntax
dlmwrite(filename, M)
dlmwrite(filename, M, 'D')
dlmwrite(filename, M, 'D', R, C)
dlmwrite(filename, M, 'attrib1', value1, 'attrib2', value2, ...)
dlmwrite(filename, M, '-append')
dlmwrite(filename, M, '-append', attribute-value list)
Use this script: msm_to_mm.m, writes an MATLAB sparse matrix to an MatrixMarket file.
And This thread may also be useful.