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.

1 Data Structures<br />

Full and Sparse Matrices<br />

It is not uncommon to have matrices with a large number of zero-valued<br />

elements and, because <strong>MATLAB</strong> stores zeros in the same way it stores any<br />

other numeric value, these elements can use memory space unnecessarily and<br />

can sometimes require extra computing time.<br />

Sparse matrices provide a way to store data that has a large percentage of<br />

zero elements more efficiently. While full matrices internally store every<br />

element in memory regardless of value, sparse matrices store only the nonzero<br />

elements and their row indices. Using sparse matrices can significantly<br />

reduce the amount of memory required for data storage.<br />

You can create sparse matrices for the double and logical data types. All<br />

<strong>MATLAB</strong> built-in arithmetic, logical, and indexing operations can be applied<br />

to sparse matrices, or to mixtures of sparse and full matrices. Operations<br />

on sparse matrices return sparse matrices and operations on full matrices<br />

return full matrices.<br />

See the section on Sparse Matrices in the <strong>MATLAB</strong> Mathematics<br />

documentation for more information onworkingwithsparsematrices.<br />

Sparse Matrix Functions<br />

Thistableshowssomeofthefunctions most commonly used when working<br />

with sparse matrices.<br />

Function<br />

full<br />

issparse<br />

nnz<br />

nonzeros<br />

nzmax<br />

spalloc<br />

sparse<br />

Description<br />

Convert a sparse matrix to a full matrix.<br />

Determine if a matrix is sparse.<br />

Return the number of nonzero matrix elements.<br />

Return the nonzero elements of a matrix.<br />

Return the amount of storage allocated for nonzero<br />

elements.<br />

Allocate spaceforasparsematrix.<br />

Create a sparse matrix or convert full to sparse.<br />

1-50

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

Saved successfully!

Ooh no, something went wrong!