5

データをファイルに書き込むことを考えています。大量のデータをファイルに書き込む方法の例を誰かが持っていますか?

編集:マトリックス内のほとんどの要素はゼロであり、他の要素はuint32です。@Jonasが提案したように、私は最も単純save()load()うまくいくと思います。

4

3 に答える 3

6

ゼロに関する編集を見た人は誰もいないと思います:)

それらがほとんどゼロである場合は、マトリックスをスパース表現に変換してから保存する必要があります。sparse関数でそれを行うことができます。

コード

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    

スパース実装はuint32.

于 2010-05-24T20:40:27.920 に答える
3

データファイルのサイズをできるだけ小さくすることに関心がある場合は、次の提案があります。

説明するためのいくつかの例を次に示します。

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.dat'は約131,073KB、'data_uint8.dat'約16,385 KB、2KB'data_sparse.dat'未満になります。

また、データを( FREADを使用して)読み戻し、適切に再形成できるように、data\vectorサイズをファイルに書き込んだことに注意してください。また、 FWRITE'double'にまたは'uint8'引数を指定しなかった場合、MATLABは、デフォルトの倍精度を使用する必要がなく、データ値を書き出すために8ビットしか使用しないことを理解するのに十分賢いでしょう(すべて0および1)。

于 2010-05-24T19:54:00.697 に答える
2

データはどのように生成されますか? どのようにデータにアクセスする必要がありますか?

正しく計算すると、変数がすべて double の場合、変数は 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')
于 2010-05-24T19:54:17.093 に答える