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.

6 Sparse Matrices<br />

This matrix requires storage for nzmax floating-point numbers and nzmax+n+1<br />

integers. At 8 bytes per floating-point number and 4 bytes per integer, the total<br />

number <strong>of</strong> bytes required to store a sparse matrix is<br />

8*nzmax + 4*(nzmax+n+1)<br />

Note that the storage requirement depends upon nzmax and the number <strong>of</strong><br />

columns, n. The memory required to store a sparse matrix containing a large<br />

number <strong>of</strong> rows but having few columns is much less that the memory required<br />

to store the transpose <strong>of</strong> this matrix:<br />

S1 = spalloc(2^20,2,1);<br />

S2 = spalloc(2,2^20,1);<br />

whos<br />

Name Size Bytes Class<br />

S1 1048576x2 24 double array (sparse)<br />

S2 2x1048576 4194320 double array (sparse)<br />

Grand total is 2 elements using 4194344 bytes<br />

Sparse matrices with complex elements are also possible. In this case,<br />

<strong>MATLAB</strong> uses a fourth array with nnz floating-point elements to store the<br />

imaginary parts <strong>of</strong> the nonzero elements. An element is considered nonzero if<br />

either its real or imaginary part is nonzero.<br />

General Storage Information<br />

The whos command provides high-level information about matrix storage,<br />

including size and storage class. For example, this whos listing shows<br />

information about sparse and full versions <strong>of</strong> the same matrix.<br />

M_full = magic(1100);<br />

M_full(M_full > 50) = 0;<br />

M_sparse = sparse(M_full);<br />

% Create 1100-by-1100 matrix.<br />

% Set elements >50 to zero.<br />

% Create sparse matrix <strong>of</strong> same.<br />

6-6

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

Saved successfully!

Ooh no, something went wrong!