您的位置:首页 > 编程语言 > MATLAB

matlab中的diag,spdiags函数

2015-11-16 17:29 471 查看

1 diag函数

1.1 定义

diag函数功能:矩阵对角元素的提取和创建对角阵。设以下X为方阵,v为向量

1.1 用法

(1)X = diag(v,k)

当v是一个含有n个元素的向量时,返回一个n+abs(k)阶方阵X,向量v在矩阵X中的第k个对角线上,

k=0表示主对角线,

k>0表示在主对角线上方,

k<0表示在主对角线下方。

例1:

v=[1 2 3];

diag(v, 3)

ans =

0 0 0 1 0 0

0 0 0 0 2 0

0 0 0 0 0 3

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

注:从主对角矩阵上方的第三个位置开始按对角线方向产生数据的

例2:

v=[1 2 3];

diag(v, -1)

ans =

0 0 0 0

1 0 0 0

0 2 0 0

0 0 3 0

注:从主对角矩阵下方的第一个位置开始按对角线方向产生数据的

(2)X = diag(v)

向量v在方阵X的主对角线上,类似于diag(v,k),k=0的情况。

例3:

v=[1 2 3];

diag(v)

ans =

1 0 0

0 2 0

0 0 3

注:写成了对角矩阵的形式

(3)v = diag(X,k)

返回列向量v,v由矩阵X的第k个对角线上的元素形成

例4:

v=[1 0 3;2 3 1;4 5 3];

diag(v,1)

ans =

0

1

注:把主对角线上方的第一个数据作为起始数据,按对角线顺序取出写成列向量形式

(4)v = diag(X)返回矩阵X的主对角线上的元素,类似于diag(X,k),k=0的情况例5:

v=[1 0 0;

0 3 0;

0 0 3];

diag(v)

ans =

1

3

3

或改为:

v=[1 0 3;2 3 1;4 5 3];

diag(v)

ans =

1

3

3

注:把主对角线的数据取出写成列向量形式

(5)diag(diag(X))

取出X矩阵的对角元,然后构建一个以X对角元为对角的对角矩阵。

例6:

X=[1 2;

3 4]

diag(diag(X))

X =

1 2

3 4

ans =

1 0

0 4

2 spdiags函数

2.1 定义

spdiags函数:Extract and create sparse band and diagonal matrices

2.2 用法

The spdiags function generalizes the function diag.

Four different operations, distinguished by the number of input arguments,are possible.

B = spdiags(A)

extracts all nonzero diagonals from the m-by-n matrix A. B is a min(m,n)-by-p matrix whose

columns are the p nonzero diagonals of A.

提取矩阵A(大小为m×n)的所有非零对角列。B的大小为 min(m,n)×p,其中p表示A的p个非零对角列

[B,d] = spdiags(A)

returns a vector d of length p, whose integer components specify the diagonals in A.

同上,这里d表示一个列向量,对应原来A的非零对角列的的标号

B = spdiags(A,d)

extracts the diagonals specified by d.

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)

creates an m-by-n sparse matrix by taking

the columns of B and placing them along the diagonals

specified by d.

Roughly, A, B, and d are related by

for k = 1:p

B(:,k) = diag(A,d(k))

end

2.3 例子

(1) For the following matrix,

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]

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

说明:

a. The columns of the first output B contain the nonzero diagonals of A. The second output d lists the indices of the nonzero diagonals of A, as shown in the following diagram.



Note that the longest nonzero diagonal in A is contained in column 3 of B. The other nonzero diagonals of A have extra zeros added to their corresponding columns in B, to give all columns of B the same length.

For the nonzero diagonals below the main diagonal of A, extra zeros are added at the tops of columns.

For the nonzero diagonals above the main diagonal of A, extra zeros are added at the bottoms of columns.

This is illustrated by the following diagram.



(2)Example 2

This example generates a sparse tridiagonal representation of the classic second difference operator on 3 points.

e = ones(3,1);

b=[e -2*e e]

A = spdiags(b, -1:1, 3, 3);

e =

1

1

1

b =

1 -2 1

1 -2 1

1 -2 1

full(A)

ans =

-2 1 0

1 -2 1

0 1 -2

(3) Example 5A

This example illustrates the use of the syntax A = spdiags(B,d,m,n), under three conditions:

m is equal to n

m is greater than n

m is less than n

The command used in this example is

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

where B is the 5-by-3 matrix shown below. The resulting matrix A has dimensions m-by-n, and has nonzero diagonals at [-2 0 2] (a sub-diagonal at -2, the main diagonal, and a super-diagonal at 2).

B =

1 6 11

2 7 12

3 8 13

4 9 14

5 10 15

The first and third columns of matrix B are used to create the sub- and super-diagonals of A respectively.

In all three cases though, these two outer columns of B are longer than the resulting diagonals of A.

Because of this, only a part of the columns is used in A.

When m == n or m > n, spdiags takes elements of the super-diagonal in A from the lower part of the corresponding column of B,

and elements of the sub-diagonal in A from the upper part of the corresponding column of B.

(注:m大于或等于n时,A的下半部分的对角列数比上半部分多,故A的下半部分填充B的对应列的上半部分,A的上半部分填充B的对应列的下半部分)

When m < n, spdiags does the opposite, taking elements of the super-diagonal in A from the upper part of the corresponding column of B,

and elements of the sub-diagonal in A from the lower part of the corresponding column of B.

(注:m小于n时,A的上半部分的对角列数比下半部分多,故A的上半部分填充B的对应列的上半部分,A的下半部分填充B的对应列的下半部分)



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 A).



参考文献:

[1]http://blog.sina.com.cn/s/blog_66d362d70101bzm2.html 博主:何勍

[2]https://www.mathworks.cn/help/matlab/ref/spdiags.html 博主:Matlab Inc.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: