MATLAB Guide

A sparse matrix is one with a large percentage of zero elements. When dealing with large, sparse matrices, it is desirable to take advantage of the sparsity by storing and operating only on the nonzeros. MATLAB has a sparse data type that stores just the nonzero entries of a matrix together with their row and column indices. In this chapter we will use the term "sparse matrix" for a matrix stored in the sparse data type and "full matrix" for a matrix stored in the (default) double data type.
Sparse matrices can be created in various ways, several of which involve the sparse function. Given a t-vector s of matrix entries and t-vectors i and j of indices, the command A ? sparse (i, j, s) defines a sparse matrix A of dimension max (i)-by- max(j) with A(i(k), j(k)) = s(k), for k=1: t and all other elements zero. Example:
<span class="unicode">?</span> A = sparse([1 2 2 4 4], [3 1 4 2 4], 1:5) A = (2,1) 2 (4,2) 4 (1,3) 1 (2,4) 3 (4,4) 5
MATLAB displays a sparse matrix by listing the nonzero...