spdiags
Extract and create sparse band and diagonal matrices
Syntax
B = spdiags(A)
[B,d] = spdiags(A)
B = spdiags(A,d)
一个= spdiags(B,d,A)
一个= spdiags(B,d,m,n)
Description
Thespdiags
function generalizes the functiondiag
. Four different operations, distinguished by the number of input arguments, are possible.
B = spdiags(A)
extracts all nonzero diagonals from them
-by-n
matrix一个
.B
is amin(m,n)
-by-p
matrix whose columns are thep
非零的对角线一个
.
[B,d] = spdiags(A)
returns a vectord
of lengthp
, whose integer components specify the diagonals in一个
.
B = spdiags(A,d)
extracts the diagonals specified byd
.
一个= spdiags(B,d,A)
replaces the diagonals specified byd
with the columns ofB
. The output is sparse.
一个= spdiags(B,d,m,n)
creates anm
-by-n
sparse matrix by taking the columns ofB
and placing them along the diagonals specified byd
.
Note
In this syntax, if a column ofB
is longer than the diagonal it is replacing, andm >= n
,spdiags
takes elements of super-diagonals from the lower part of the column ofB
, and elements of sub-diagonals from the upper part of the column ofB
. However, ifm < n
, then super-diagonals are from the upper part of the column ofB
, and sub-diagonals from the lower part. (SeeExample 5AandExample 5B, below).
一个rguments
Thespdiags
function deals with three matrices, in various combinations, as both input and output.
一个 |
一个n |
B |
一个 |
d |
一个vector of length |
Roughly,一个
,B
, andd
are related by
for k = 1:p B(:,k) = diag(A,d(k)) end
Some elements ofB
, corresponding to positions outside of一个
, are not defined by these loops. They are not referenced whenB
is input and are set to zero whenB
is output.
How the Diagonals of A are Listed in the Vector d
一个n m-by-n matrix一个
has m+n-1diagonals. These are specified in the vectord
using indices from -m+1 to n-1. For example, if一个
is 5-by-6, it has 10 diagonals, which are specified in the vectord
using the indices -4, -3 , ... 4, 5. The following diagram illustrates this for a vector of all ones.
例子
Example 1
For the following matrix,
一个=[0 5 0 10 0 0;... 0 0 6 0 11 0;... 3 0 0 7 0 12;... 1 4 0 0 8 0;... 0 2 5 0 0 9] A = 0 5 0 10 0 0 0 0 6 0 11 0 3 0 0 7 0 12 1 4 0 0 8 0 0 2 5 0 0 9
the command
[B, d] =spdiags(A)
returns
B = 0 0 5 10 0 0 6 11 0 3 7 12 1 4 8 0 2 5 9 0 d = -3 -2 1 3
The columns of the first outputB
contain the nonzero diagonals of一个
. The second outputd
lists the indices of the nonzero diagonals of一个
, as shown in the following diagram. SeeHow the Diagonals of A are Listed in the Vector d.
Note that the longest nonzero diagonal in一个
is contained in column 3 ofB
. The other nonzero diagonals of一个
have extra zeros added to their corresponding columns inB
, to give all columns ofB
the same length. For the nonzero diagonals below the main diagonal of一个
, extra zeros are added at the tops of columns. For the nonzero diagonals above the main diagonal of一个
, extra zeros are added at the bottoms of columns. This is illustrated by the following diagram.
Example 2
This example generates a sparse tridiagonal representation of the classic second difference operator onn
points.
e = ones(n,1); A = spdiags([e -2*e e], -1:1, n, n)
Turn it into Wilkinson's test matrix (seegallery
):
一个= spdiags(abs(-(n-1)/2:(n-1)/2)',0,A)
Finally, recover the three diagonals:
B = spdiags(A)
Example 3
The second example is not square.
一个= [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]
Herem =7
,n = 4
, andp = 3
.
The statement[B,d] = spdiags(A)
producesd = [-3 0 2]'
and
B = [41 11 0 52 22 0 63 33 13 74 44 24]
Conversely, with the aboveB
andd
, the expressionspdiags(B,d,7,4)
reproduces the original一个
.
Example 4
This example shows howspdiags
creates the diagonals when the columns ofB
are longer than the diagonals they are replacing.
B = repmat((1:6)',[1 7]) B = 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6 d = [-4 -2 -1 0 3 4 5]; A = spdiags(B,d,6,6); full(A) ans = 1 0 0 4 5 6 1 2 0 0 5 6 1 2 3 0 0 6 0 2 3 4 0 0 1 0 3 4 5 0 0 2 0 4 5 6
Example 5A
This example illustrates the use of the syntax一个= spdiags(B,d,m,n)
, under three conditions:
m
is equal ton
m
is greater thann
m
is less thann
The command used in this example is
一个= full(spdiags(B, [-2 0 2], m, n))
whereB
is the 5-by-3 matrix shown below. The resulting matrix一个
has dimensionsm
-by-n
, and has nonzero diagonals at[-2 0 2]
(a sub-diagonal at-2
, the main diagonal, and a super-diagonal at2
).
B = [1 6 11 2 7 12 3 8 13 4 9 14 5 10 15]
The first and third columns of matrixB
are used to create the sub- and super-diagonals of一个
respectively. In all three cases though, these two outer columns ofB
are longer than the resulting diagonals of一个
. Because of this, only a part of the columns are used in一个
.
Whenm == n
orm > n
,spdiags
takes elements of the super-diagonal in一个
from the lower part of the corresponding column ofB
, and elements of the sub-diagonal in一个
from the upper part of the corresponding column ofB
.
一个= full(spdiags(B, [-2 0 2], 5, 5)) Matrix B Matrix A 1 6 11 6 0 13 0 0 2 7 12 0 7 0 14 0 3 8 13 == spdiags => 1 0 8 0 15 4 9 14 0 2 0 9 0 5 10 15 0 0 3 0 10
一个(3,1)
,(4,2)
, and一个(5,3)
are taken from the upper part ofB(:,1)
.
一个(1,3)
,一个(2,4)
, and一个(3,5)
are taken from the lower part ofB(:,3)
.
一个= full(spdiags(B, [-2 0 2], 5, 4)) Matrix B Matrix A 1 6 11 6 0 13 0 2 7 12 0 7 0 14 3 8 13 == spdiags => 1 0 8 0 4 9 14 0 2 0 9 5 10 15 0 0 3 0
Same as in Part A.
Whenm < n
,spdiags
does the opposite, taking elements of the super-diagonal in一个
from the upper part of the corresponding column ofB
, and elements of the sub-diagonal in一个
from the lower part of the corresponding column ofB
.
一个= full(spdiags(B, [-2 0 2], 4, 5)) Matrix B Matrix A 1 6 11 6 0 11 0 0 2 7 12 0 7 0 12 0 3 8 13 == spdiags => 3 0 8 0 13 4 9 14 0 4 0 9 0 5 10 15
一个(3,1)
and(4,2)
are taken from the lower part ofB(:,1)
.
一个(1,3)
,一个(2,4)
, and一个(3,5)
are taken from the upper part ofB(:,3)
.
Example 5B
Extract the diagonals from the first part of this example back into a column format using the command
B = spdiags(A)
You can see that in each case the original columns are restored (minus those elements that had overflowed the super- and sub-diagonals of matrix一个
).
Matrix A Matrix B 6 0 13 0 0 1 6 0 0 7 0 14 0 2 7 0 1 0 8 0 15 == spdiags => 3 8 13 0 2 0 9 0 0 9 14 0 0 3 0 10 0 10 15
Matrix A Matrix B 6 0 13 0 1 6 0 0 7 0 14 2 7 0 1 0 8 0 == spdiags => 3 8 13 0 2 0 9 0 9 14 0 0 3 0
Matrix A Matrix B 6 0 11 0 0 0 6 11 0 7 0 12 0 0 7 12 3 0 8 0 13 == spdiags => 3 8 13 0 4 0 9 0 4 9 0