How to | Work with Sparse Matrices
Sparse representations of matrices are useful because they do not store every element. If one particular value appears very frequently, it can be very advantageous to use a sparse representation. The Wolfram Language offers a sparse representation for matrices, vectors, and tensors with SparseArray. These are very closely related to dense matrices, which are represented by lists. Most operations that work for lists also work for sparse arrays.
There are a number of ways to create sparse arrays. Many of them make use of the function SparseArray.
More complicated matrices can be made with Band. In this example, a band is created along the antidiagonal:
Functions that work with dense matrices typically work with sparse matrices. Often they will use special sparse matrix techniques that are faster and work without ever converting to a dense matrix. This leads to large savings of time and memory.
Sometimes it is useful to get a view of the location of the nonzero elements in a sparse matrix. This can be done with MatrixPlot. Even if the matrix has many elements, MatrixPlot provides an efficient view of the sparsity pattern of a matrix.
This loads a large matrix with Import: