MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

only 4 bytes. There are some limitations on what you can do with the single class, but
most MATLAB Math operations are supported.


If you just need to carry out simple arithmetic and you represent the original data as
integers, you can use the integer classes in MATLAB. The following is a list of numeric
classes, memory requirements (in bytes), and the supported operations.


Class (Data Type) Bytes Supported Operations
single 4 Most math
double 8 All math
logical 1 Logical/conditional operations
int8, uint8 1 Arithmetic and some simple functions
int16, uint16 2 Arithmetic and some simple functions
int32, uint32 4 Arithmetic and some simple functions
int64, int64 8 Arithmetic and some simple functions

Reduce the Amount of Overhead When Storing Data


MATLAB arrays (implemented internally as mxArrays) require room to store meta
information about the data in memory, such as type, dimensions, and attributes. This
takes about 80 bytes per array. This overhead only becomes an issue when you have a
large number (e.g., hundreds or thousands) of small mxArrays (e.g., scalars). The whos
command lists the memory used by variables, but does not include this overhead.


Because simple numeric arrays (comprising one mxArray) have the least overhead, you
should use them wherever possible. When data is too complex to store in a simple array
(or matrix), you can use other data structures.


Cell arrays are comprised of separate mxArrays for each element. As a result, cell arrays
with many small elements have a large overhead.


Structures require a similar amount of overhead per field (see “Array Headers” on page
29-14). Structures with many fields and small contents have a large overhead and should
be avoided. A large array of structures with numeric scalar fields requires much more
memory than a structure with fields containing large numeric arrays.


Also note that while MATLAB stores numeric arrays in contiguous memory, this is not the
case for structures and cell arrays.


Strategies for Efficient Use of Memory
Free download pdf