Mathematics 
Creating Sparse Matrices
MATLAB never creates sparse matrices automatically. Instead, you must determine if a matrix contains a large enough percentage of zeros to benefit from sparse techniques.
The density of a matrix is the number of nonzero elements divided by the total number of matrix elements. Matrices with very low density are often good candidates for use of the sparse format.
Converting Full to Sparse
You can convert a full matrix to sparse storage using the sparse
function with a single argument.
The printed output lists the nonzero elements of S
, together with their row and column indices. The elements are sorted by columns, reflecting the internal data structure.
You can convert a sparse matrix to full storage using the full
function, provided the matrix order is not too large. For example A = full(S)
reverses the example conversion.
Converting a full matrix to sparse storage is not the most frequent way of generating sparse matrices. If the order of a matrix is small enough that full storage is possible, then conversion to sparse storage rarely offers significant savings.
Creating Sparse Matrices Directly
You can create a sparse matrix from a list of nonzero elements using the sparse
function with five arguments.
i
and j
are vectors of row and column indices, respectively, for the nonzero elements of the matrix. s
is a vector of nonzero values whose indices are specified by the corresponding (i,j)
pairs. m
is the row dimension for the resulting matrix, and n
is the column dimension.
The matrix S
of the previous example can be generated directly with
The sparse
command has a number of alternate forms. The example above uses a form that sets the maximum number of nonzero elements in the matrix to length(s)
. If desired, you can append a sixth argument that specifies a larger maximum, allowing you to add nonzero elements later without reallocating the sparse matrix.
Example: Generating a Second Difference Operator
The matrix representation of the second difference operator is a good example of a sparse matrix. It is a tridiagonal matrix with 2s on the diagonal and 1s on the super and subdiagonal. There are many ways to generate it  here's one possibility.
For n = 5
, MATLAB responds with
S = (1,1) 2 (2,1) 1 (1,2) 1 (2,2) 2 (3,2) 1 (2,3) 1 (3,3) 2 (4,3) 1 (3,4) 1 (4,4) 2 (5,4) 1 (4,5) 1 (5,5) 2
Now F = full(S)
displays the corresponding full matrix.
Creating Sparse Matrices from Their Diagonal Elements
Creating sparse matrices based on their diagonal elements is a common operation, so the function spdiags
handles this task. Its syntax is
To create an output matrix S
of size mbyn with elements on p
diagonals:
B
is a matrix of size min(
m,
n)
byp. The columns of B
are the values to populate the diagonals of S
.
d
is a vector of length p
whose integer elements specify which diagonals of S
to populate.
That is, the elements in column j
of B
fill the diagonal specified by element j
of d
.
Note
If a column of B is longer than the diagonal it's replacing, superdiagonals are taken from the lower part of the column of B , and subdiagonals are taken from the upper part of the column of B .

As an example, consider the matrix B
and the vector d
.
Use these matrices to create a 7by4 sparse matrix A
.
A = spdiags(B,d,7,4) A = (1,1) 11 (4,1) 41 (2,2) 22 (5,2) 52 (1,3) 13 (3,3) 33 (6,3) 63 (2,4) 24 (4,4) 44 (7,4) 74
In its full form, A
looks like this.
spdiags
can also extract diagonal elements from a sparse matrix, or replace matrix diagonal elements with new values. Type help
spdiags
for details.
Introduction  Importing Sparse Matrices from Outside MATLAB 