23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Memory Efficiently<br />

Numeric Arrays<br />

<strong>MATLAB</strong> requires 1, 2, 4, or 8 bytes to store 8-bit, 16-bit, 32-bit, and 64-bit<br />

signed and unsigned integers respectively. For floating-point numbers,<br />

<strong>MATLAB</strong> uses 4 or 8 bytes for single and double types. To conserve<br />

memory, The MathWorks recommends that you use the smallest integer or<br />

floating-point type that will contain your data without overflowing. For more<br />

information, see "Numeric Types" in the <strong>MATLAB</strong> <strong>Programming</strong> section on<br />

Data Types.<br />

Complex Arrays<br />

<strong>MATLAB</strong> stores complex data as separate real and imaginary parts. If you<br />

makeacopyofacomplexarrayvariable,andthenmodifyonlytherealor<br />

imaginary part of the array, <strong>MATLAB</strong> creates a new array containing both<br />

real and imaginary parts.<br />

Sparse Matrices<br />

It is best to store matrices with values that are mostly zero in sparse format.<br />

Sparse matrices can use less memory and may also be faster to manipulate<br />

than full matrices. You can convert a full matrix to sparse format using the<br />

sparse function.<br />

Compare two 1000-by-1000 matrices: X, a matrix of doubles with 2/3 of<br />

itselementsequaltozero;andY, a sparse copy of X. Asshownbelow,<br />

approximately half as much memory is required for the sparse matrix:<br />

whos<br />

Name Size Bytes Class<br />

X 1000x1000 8000000 double array<br />

Y 1000x1000 4004000 double array (sparse)<br />

Memory Management Functions<br />

The following functions can help you to manage memory use in <strong>MATLAB</strong>:<br />

• whos shows how much memory has been allocated for variables in the<br />

workspace.<br />

11-17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!