User:Eml4500.f08.gravy.JMT

19. Sparse Matrix Computations

There are two storage modes in Matlab for a matrix: full and sparse. Full mode stores everything, while sparse only stores nonzero numbers. The default is to store elements of a matrix in full mode. In full mode, the zero elements are taken into account during calculations. Sparse mode will remove these zero elements during calculations, thus reducing time spent performing them. It saves time, but still outputs the same numbers.

Matrices can be converted between sparse and full fairly easily. The command “S=sparse(F)” will convert the full matrix, F, to a sparse matrix, S. It can then be converted back from sparse to full using the command "F=full(S)”.  Usually though, if a sparse matrix is desired, it will be created directly rather than by converting from a full matrix.  There are sparse analogs to many full matrices.  The fuctions speye, sparse, spones, and sprandn are sparse analogs to the full functions eye, zeros, ones and randn respectively.

When performing calculations using both full and sparse matrices, the storage mode of the output will vary. If calculations are performed on only sparse functions, the output will be sparse. If calculations are performed only on full functions, the output will be full. However, if calculations are performed using both full and sparse functions, the output will always come out as full unless the user specifically states otherwise.