15.11.2014 Views

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction<br />

whos<br />

Name Size Bytes Class<br />

M_full 1100x1100 9680000 double array<br />

M_sparse 1100x1100 5004 double array (sparse)<br />

Grand total is 1210050 elements using 9685004 bytes<br />

Notice that the number <strong>of</strong> bytes used is much less in the sparse case, because<br />

zero-valued elements are not stored.<br />

Creating Sparse Matrices<br />

<strong>MATLAB</strong> never creates sparse matrices automatically. Instead, you must<br />

determine if a matrix contains a large enough percentage <strong>of</strong> zeros to benefit<br />

from sparse techniques.<br />

The density <strong>of</strong> a matrix is the number <strong>of</strong> non-zero elements divided by the total<br />

number <strong>of</strong> matrix elements. For matrix M, this would be<br />

nnz(M) / prod(size(M));<br />

Matrices with very low density are <strong>of</strong>ten good candidates for use <strong>of</strong> the sparse<br />

format.<br />

Converting Full to Sparse<br />

You can convert a full matrix to sparse storage using the sparse function with<br />

a single argument.<br />

S = sparse(A)<br />

For example<br />

A = [ 0 0 0 5<br />

0 2 0 0<br />

1 3 0 0<br />

0 0 4 0];<br />

S = sparse(A)<br />

produces<br />

6-7

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

Saved successfully!

Ooh no, something went wrong!