## Sparse Matrix Computations

An m-by-n sparse matrix is stored as a set of sparse columns, where each column is represented as a packed list of nonzero values and their row indices. Thus, if A is sparse, x=A(9,:) takes much more time than x=A(:,9), and s=A(4,5) is also slow. To get high performance when

“8primer” — — #137

dealing with sparse matrices, use matrix expressions instead of for loops and vector or scalar expressions. If you must operate on the rows of a sparse matrix A, compute the transpose (C=A’) and work with the columns of C instead. If a full tridiagonal matrix F is created via, say,

F = randi([0 9], 6)

F = triu(tril(F,1), -1)

then the statement S=sparse(F) converts F to sparse mode. Try it. Note that the output lists the nonzero entries in column major order along with their row and column indices because of how sparse matrices are stored. The statement F=full(S) returns F in full storage mode. You can check the storage mode of a matrix A with the command issparse(A).