What is the best way to store a 16 × (2 ^ 20) matrix in MATLAB?

I am going to write data to a file. Does anyone have an example of how to write a large amount of data to a file?

Edit: Most of the elements in the matrix are zeros, others are uint32. I think the simplest save(), and load()will work as suggested @Jonas.

+5
source share
3 answers

I think no one has seen a change about zeros :)

If they are basically zeros, you must convert your matrix to its sparse representation and then save it. You can do this with the sparse function .

Code

z = zeros(10000,10000);
z(123,456) = 1;
whos z
z = sparse(z);
whos z

Output

Name          Size                   Bytes  Class     Attributes

  z         10000x10000            800000000  double  

Name          Size               Bytes  Class     Attributes

  z         10000x10000            40016  double    sparse    

, uint32.

+6

, , :

:

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 073 , 'data_uint8.dat' 16,385 , 'data_sparse.dat' 2 .

, \ , ( FREAD) . , 'double' 'uint8' FWRITE, MATLAB , 8 ( 0 1).

+3

? ?

, 200 , . , .mat, Matlab.

%# 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')
+2

All Articles