matlab中的diag,spdiags函式
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
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
(2)Example 2說明:
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.
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).
參考文獻: