MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Import Data to the Appropriate MATLAB Class

When reading data from a binary file with fread, it is a common error to specify only the
class of the data in the file, and not the class of the data MATLAB uses once it is in the
workspace. As a result, the default double is used even if you are reading only 8-bit
values. For example,

fid = fopen('large_file_of_uint8s.bin', 'r');
a = fread(fid, 1e3, 'uint8'); % Requires 8k
whos a
Name Size Bytes Class Attributes

a 1000x1 8000 double

a = fread(fid, 1e3, 'uint8=>uint8'); % Requires 1k
whos a
Name Size Bytes Class Attributes

a 1000x1 1000 uint8

Make Arrays Sparse When Possible

If your data contains many zeros, consider using sparse arrays, which store only nonzero
elements. The following example compares the space required for storage of an array of
mainly zeros:

A = eye(1000); % Full matrix with ones on the diagonal
As = sparse(A); % Sparse matrix with only nonzero elements
whos
Name Size Bytes Class Attributes

A 1000x1000 8000000 double
As 1000x1000 24008 double sparse

You can see that this array requires only approximately 4 KB to be stored as sparse, but
approximately 8 MB as a full matrix. In general, for a sparse double array with nnz
nonzero elements and ncol columns, the memory required is


  • 16 nnz + 8 ncol + 8 bytes (on a 64-bit machine)

  • 12 nnz + 4 ncol + 4 bytes (on a 32-bit machine)


Note that MATLAB does not support all mathematical operations on sparse arrays.

29 Memory Usage

Free download pdf