Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Full and Sparse Matrices

Overview

It is not uncommon to have matrices with a large number of zero-valued elements and, because the MATLAB® software stores zeros in the same way it stores any other numeric value, these elements can use memory space unnecessarily and can sometimes require extra computing time.

Sparse matrices provide a way to store data that has a large percentage of zero elements more efficiently. While full matrices internally store every element in memory regardless of value, sparse matrices store only the nonzero elements and their row indices. Using sparse matrices can significantly reduce the amount of memory required for data storage.

You can create sparse matrices for the double and logical classes. All MATLAB built-in arithmetic, logical, and indexing operations can be applied to sparse matrices, or to mixtures of sparse and full matrices. Operations on sparse matrices return sparse matrices and operations on full matrices return full matrices.

For more information about the computational advantages and construction of sparse matrices, see Memory Management and Creating Sparse Matrices.

Sparse Matrix Functions

This table shows some of the functions most commonly used when working with sparse matrices.

Function

Description

full

Convert a sparse matrix to a full matrix.

issparse

Determine if a matrix is sparse.

nnz

Return the number of nonzero matrix elements.

nonzeros

Return the nonzero elements of a matrix.

nzmax

Return the amount of storage allocated for nonzero elements.

spalloc

Allocate space for a sparse matrix.

sparse

Create a sparse matrix or convert full to sparse.

speye

Create a sparse identity matrix.

sprand

Create a sparse uniformly distributed random matrix.

Was this topic helpful?