Documentation

Constructing Sparse Matrices

Creating Sparse Matrices

MATLAB®never createssparse matrices automatically. Instead, you must determine if a matrix contains a large enough percentage of zeros to benefit from sparse techniques.

Thedensityof a matrix is the number of nonzero elements divided by the total number of matrix elements. For matrixM, this would be

nnz(M) / prod(size(M));
or
nnz(M) / numel(M);

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 thesparsefunction with a single argument.

S = sparse(A)

For example:

A = [ 0 0 0 5 0 2 0 0 1 3 0 0 0 0 4 0]; S = sparse(A)

produces

S = (3,1) 1 (2,2) 2 (3,2) 3 (4,3) 4 (1,4) 5

The printed output lists the nonzero elements ofS, 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 thefullfunction, 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 thesparsefunction with five arguments.

S = sparse(i,j,s,m,n)

iandjare vectors of row and column indices, respectively, for the nonzero elements of the matrix.sis a vector of nonzero values whose indices are specified by the corresponding(i,j)pairs.mis the row dimension of the resulting matrix, andnis the column dimension.

The matrixSof the previous example can be generated directly with

S = sparse([3 2 3 4 1],[1 2 2 3 4],[1 2 3 4 5],4,4) S = (3,1) 1 (2,2) 2 (3,2) 3 (4,3) 4 (1,4) 5

Thesparsecommand has a number of alternate forms. The example above uses a form that sets the maximum number of nonzero elements in the matrix tolength(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.

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.

D = sparse(1:n,1:n,-2*ones(1,n),n,n); E = sparse(2:n,1:n-1,ones(1,n-1),n,n); S = E+D+E'

Forn = 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

NowF = full(S)displays the corresponding full matrix.

F = full(S) F = -2 1 0 0 0 1 -2 1 0 0 0 1 -2 1 0 0 0 1 -2 1 0 0 0 1 -2

Creating Sparse Matrices from Their Diagonal Elements

Creating sparse matrices based on their diagonal elements is a common operation, so the functionspdiagshandles this task. Its syntax is

S = spdiags(B,d,m,n)

To create an output matrixS的大小m-by-nwith elements onpdiagonals:

  • Bis a matrix of sizemin(m,n)-by-p. The columns ofBare the values to populate the diagonals ofS.

  • dis a vector of lengthpwhose integer elements specify which diagonals ofSto populate.

That is, the elements in columnjofBfill the diagonal specified by elementjofd.

Note

If a column ofBis longer than the diagonal it's replacing, super-diagonals are taken from the lower part of the column ofB, and sub-diagonals are taken from the upper part of the column ofB.

As an example, consider the matrixBand the vectord.

11 B = [41 0 52 22 0 63 33 74 44 24);d = [30 2];

Use these matrices to create a 7-by-4 sparse matrixA:

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,Alooks like this:

full(A) ans = 11 0 13 0 0 22 0 24 0 0 33 0 41 0 0 44 0 52 0 0 0 0 63 0 0 0 0 74

spdiagscan also extract diagonal elements from a sparse matrix, or replace matrix diagonal elements with new values. Type帮助spdiagsfor details.

进口稀疏矩阵

You can import sparse matrices from computations outside the MATLAB environment. Use thespconvertfunction in conjunction with theloadcommand to import text files containing lists of indices and nonzero elements. For example, consider a three-column text fileT.datwhose first column is a list of row indices, second column is a list of column indices, and third column is a list of nonzero values. These statements loadT.datinto MATLAB and convert it into a sparse matrixS:

load T.dat S = spconvert(T)

Thesaveandloadcommands can also process sparse matrices stored as binary data in MAT-files.

See Also

|

Related Topics