存储16×;MATLAB中的(2^20)矩阵?
我正在考虑将数据写入一个文件。有人举过如何将大量数据写入文件的例子吗存储16×;MATLAB中的(2^20)矩阵?,matlab,matrix,file-io,bigdata,Matlab,Matrix,File Io,Bigdata,我正在考虑将数据写入一个文件。有人举过如何将大量数据写入文件的例子吗 编辑:矩阵中的大多数元素为零,其他元素为uint32。我想最简单的save()和load()也可以,正如@Jonas所建议的那样。如果您希望保持数据文件的大小尽可能小,下面是一些建议: 将数据写入二进制文件(即使用),而不是文本文件(即使用) 如果数据包含所有整数值,请将其转换为或保存为,而不是MATLAB使用的默认值 如果数据包含浮点值,但不需要默认值的范围或分辨率,请将其转换为或另存为 如果数据足够稀疏(即矩阵中的零比非
编辑:矩阵中的大多数元素为零,其他元素为
uint32
。我想最简单的save()
和load()
也可以,正如@Jonas所建议的那样。如果您希望保持数据文件的大小尽可能小,下面是一些建议:
- 将数据写入二进制文件(即使用),而不是文本文件(即使用)
- 如果数据包含所有整数值,请将其转换为或保存为,而不是MATLAB使用的默认值
- 如果数据包含浮点值,但不需要默认值的范围或分辨率,请将其转换为或另存为
- 如果数据足够稀疏(即矩阵中的零比非零多得多),则可以使用函数获取非零值的行和列索引,然后将其保存到文件中
data = double(rand(16,2^20) <= 0.00001); %# A large but very sparse matrix
%# Writing the values as type double:
fid = fopen('data_double.dat','w'); %# Open the file
fwrite(fid,size(data),'uint32'); %# Write the matrix size (2 values)
fwrite(fid,data,'double'); %# Write the data as type double
fclose(fid); %# Close the file
%# Writing the values as type uint8:
fid = fopen('data_uint8.dat','w'); %# Open the file
fwrite(fid,size(data),'uint32'); %# Write the matrix size (2 values)
fwrite(fid,data,'uint8'); %# Write the data as type uint8
fclose(fid); %# Close the file
%# Writing out only the non-zero values:
[rowIndex,columnIndex,values] = find(data); %# Get the row and column indices
%# and the non-zero values
fid = fopen('data_sparse.dat','w'); %# Open the file
fwrite(fid,numel(values),'uint32'); %# Write the length of the vectors (1 value)
fwrite(fid,rowIndex,'uint32'); %# Write the row indices
fwrite(fid,columnIndex,'uint32'); %# Write the column indices
fwrite(fid,values,'uint8'); %# Write the non-zero values
fclose(fid); %# Close the file
data=double(rand(16,2^20)数据是如何生成的?您需要如何访问数据
如果我计算正确,如果变量都是双精度的,那么它就小于200MB。因此,如果只需要从Matlab访问它,您可以轻松地将其保存并加载为单个.mat文件
%# create data
data = zeros(16,2^20);
%# save data
save('myFile.mat','data');
%# clear data to test everything works
clear data
%# load data
load('myFile.mat')
我想没人看过关于零的编辑:)
如果它们大多为零,则应将矩阵转换为稀疏表示形式,然后保存它。你可以用这个函数来实现
代码
输出
我不认为稀疏实现是为了在稀疏uint32上处理uint32
正确,但是,double应该有一个可接受的范围。对,我想强调的是,原始数据是uint32
不会有帮助。
z = zeros(10000,10000);
z(123,456) = 1;
whos z
z = sparse(z);
whos z
Name Size Bytes Class Attributes
z 10000x10000 800000000 double
Name Size Bytes Class Attributes
z 10000x10000 40016 double sparse