如何将非常大的 MATLAB 稀疏矩阵保存到文本文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I save a very large MATLAB sparse matrix to a text file?
提问by Midhat
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()on this monster results in an Out of Memoryerror.
How do I export it?
我在 MATLAB(版本 7)中有一个 30000x14000 的稀疏矩阵,我需要在另一个程序中使用它。调用 save 不会将其写为 ASCII(不支持)。调用full()这个怪物会导致 Out of Memory错误。
我如何导出它?
采纳答案by Midhat
I saved it as text using Java within MATLAB. MATLAB Code:
我在 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();
Here datais an extremely large sparse matrix.
这data是一个非常大的稀疏矩阵。
回答by Matt
You can use find to get index & value vectors:
您可以使用 find 来获取索引和值向量:
[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):
您可以使用 spconvert 从 data_dump 重新创建数据,这意味着“从稀疏矩阵外部格式导入”(所以我想这是一个很好的导出格式):
data = spconvert( data_dump )
You can save to ascii with:
您可以使用以下命令保存为 ascii:
save -ascii data.txt data_dump
But this dumps indices as double, you can write it out more nicely with fopen/fprintf/fclose:
但这会将索引转储为双倍,您可以使用 fopen/fprintf/fclose 更好地将其写出来:
fid = fopen('data.txt','w')
fprintf( fid,'%d %d %f\n', transpose(data_dump) )
fclose(fid)
Hope this helps.
希望这可以帮助。
回答by Vebjorn Ljosa
Save the sparse matrix as a .matfile. Then, in the other program, use a suitable library to read the .matfile.
将稀疏矩阵保存为.mat文件。然后,在另一个程序中,使用合适的库来读取.mat文件。
For instance, if the other program is written in Python, you can use the scipy.io.mio.loadmatfunction, which supports sparse arrays and gives you a sparse numpy matrix.
例如,如果另一个程序是用 Python 编写的,您可以使用该scipy.io.mio.loadmat函数,该函数支持稀疏数组并为您提供一个稀疏的 numpy 矩阵。
回答by Mr Fooz
Use the findfunction to get the indices of non-zero elements...
使用该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 ind2subto convert the linear indices to row, column subscripts.
如果需要,您可以使用ind2sub将线性索引转换为行、列下标。
If you need to recreate a sparse matrix in matlab from subscripts + values, use spconvert.
如果您需要从下标 + 值在 matlab 中重新创建稀疏矩阵,请使用spconvert.
回答by Veynom
Did you try partitioning it ?
你试过分区吗?
I mean try calling full() on the 1000 first rows (or 5000) and then repeat the process if it works.
我的意思是尝试在前 1000 行(或 5000 行)上调用 full(),然后如果有效,则重复该过程。
回答by ehsan
dlmwrite- Write matrix to ASCII-delimited file Syntax
dlmwrite- 将矩阵写入 ASCII 分隔的文件语法
dlmwrite(filename, M)
dlmwrite(文件名,M)
dlmwrite(filename, M, 'D')
dlmwrite(文件名,M,'D')
dlmwrite(filename, M, 'D', R, C)
dlmwrite(文件名,M,'D',R,C)
dlmwrite(filename, M, 'attrib1', value1, 'attrib2', value2, ...)
dlmwrite(文件名, M, 'attrib1', value1, 'attrib2', value2, ...)
dlmwrite(filename, M, '-append')
dlmwrite(文件名, M, '-append')
dlmwrite(filename, M, '-append', attribute-value list)
dlmwrite(filename, M, '-append', 属性值列表)
回答by Dr_Hope
Use this script: msm_to_mm.m, writes an MATLAB sparse matrix to an MatrixMarket file.
使用此脚本: msm_to_mm.m,将 MATLAB 稀疏矩阵写入 MatrixMarket 文件。
And This threadmay also be useful.
这个线程也可能有用。
回答by Scottie T
If this is pretty much a one time deal, then I would just iterate through the matrix and write the matrix to an ASCII file by brute force, or else use @Veynom'ssuggestion and call full() on a subset of rows. It may take a while, but it will probably be done faster than it might take to learn how to read in a .mat file outside of the MATLAB environment.
如果这几乎是一次性交易,那么我将遍历矩阵并通过蛮力将矩阵写入 ASCII 文件,或者使用 @ Veynom 的建议并在行的子集上调用 full()。这可能需要一段时间,但可能比学习如何在 MATLAB 环境之外读取 .mat 文件所需的时间更快。
If this is something you need to do on a recurring basis, then I would take @Vebjorn's advice and use a library to read the .mat file.
如果这是您需要经常做的事情,那么我会接受@ Vebjorn的建议并使用库来读取 .mat 文件。

