Documentation

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

Thespdiagsfunction 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-nmatrix一个.Bis amin(m,n)-by-pmatrix whose columns are thep非零的对角线一个.

[B,d] = spdiags(A)returns a vectordof 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 bydwith the columns ofB. The output is sparse.

一个= spdiags(B,d,m,n)creates anm-by-nsparse matrix by taking the columns ofBand placing them along the diagonals specified byd.

Note

In this syntax, if a column ofBis longer than the diagonal it is replacing, andm >= n,spdiagstakes 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

Thespdiagsfunction deals with three matrices, in various combinations, as both input and output.

一个

一个nm-by-nmatrix, usually (but not necessarily) sparse, with its nonzero or specified elements located onpdiagonals.

B

一个min(m,n)-by-pmatrix, usually (but not necessarily) full, whose columns are the diagonals of一个.

d

一个vector of lengthpwhose integer components specify the diagonals in一个.

Roughly,一个,B, anddare 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 whenBis input and are set to zero whenBis 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 vectordusing indices from -m+1 to n-1. For example, if一个is 5-by-6, it has 10 diagonals, which are specified in the vectordusing 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 outputBcontain the nonzero diagonals of一个. The second outputdlists 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 ofBthe 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 onnpoints.

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 aboveBandd, the expressionspdiags(B,d,7,4)reproduces the original一个.

Example 4

This example shows howspdiagscreates the diagonals when the columns ofBare 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:

  • mis equal ton

  • mis greater thann

  • mis less thann

The command used in this example is

一个= full(spdiags(B, [-2 0 2], m, n))

whereBis 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 matrixBare used to create the sub- and super-diagonals of一个respectively. In all three cases though, these two outer columns ofBare longer than the resulting diagonals of一个. Because of this, only a part of the columns are used in一个.

Whenm == norm > n,spdiagstakes 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,spdiagsdoes 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

See Also

|

Introduced before R2006a

Was this topic helpful?