|MATLAB Function Reference|
Extract and create sparse band and diagonal matrices
spdiags function generalizes the function
diag. Four different operations, distinguished by the number of input arguments, are possible:
[B,d] = spdiags(A)
extracts all nonzero diagonals from the
B is a
p matrix whose columns are the
p nonzero diagonals of
d is a vector of length
p whose integer components specify the diagonals in
B = spdiags(A,d)
extracts the diagonals specified by
A = spdiags(B,d,A)
replaces the diagonals specified by
d with the columns of
B. The output is sparse.
A = spdiags(B,d,m,n)
n sparse matrix by taking the columns of
B and placing them along the diagonals specified by
If a column of |
spdiags function deals with three matrices, in various combinations, as both input and output.
||A vector of length
d are related by
Some elements of
B, corresponding to positions outside of
A, are not defined by these loops. They are not referenced when
B is input and are set to zero when
B is output.
Example 1. This example generates a sparse tridiagonal representation of the classic second difference operator on
Turn it into Wilkinson's test matrix (see
Finally, recover the three diagonals:
Example 2. The second example is not square.
Conversely, with the above
d, the expression
spdiags(B,d,7,4) reproduces the original
Example 3. This example shows how
spdiags creates the diagonals when the columns of
B are longer than the diagonals they are replacing.