MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Complex Arrays


MATLAB stores complex data as separate real and imaginary parts. If you make a copy of
a complex array variable, and then modify only the real or imaginary part of the array,
MATLAB creates an array containing both real and imaginary parts.


Sparse Matrices


It is best to store matrices with values that are mostly zero in sparse format. Sparse
matrices can use less memory and might also be faster to manipulate than full matrices.
You can convert a full matrix to sparse format using the sparse function.


Compare two 1000-by-1000 matrices: X, a matrix of doubles with 2/3 of its elements equal
to zero; and Y, a sparse copy of X. The following example shows that the sparse matrix
requires approximately half as much memory:


whos
Name Size Bytes Class


X 1000x1000 8000000 double array
Y 1000x1000 4004000 double array (sparse)


Cell Arrays


In addition to data storage, cell arrays require a certain amount of additional memory to
store information describing each cell. This information is recorded in a header, and there
is one header for each cell of the array. You can determine the amount of memory
required for a cell array header by finding the number of bytes consumed by a 1-by-1 cell
that contains no data, as shown below for a 32-bit system:


A = {[]}; % Empty cell array


whos A
Name Size Bytes Class Attributes


A 1x1 60 cell


In this case, MATLAB shows the number of bytes required for each header in the cell
array on a 32-bit system to be 60. This is the header size that is used in all of the 32-bit
examples in this section. For 64-bit systems, the header size is assumed to be 112 bytes in
this documentation. You can find the correct header size on a 64-bit system using the
method just shown for 32 bits.


How MATLAB Allocates Memory
Free download pdf