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

matlab diag 函数

2016-05-05 14:45 911 查看
Create a 1-by-5 vector.

把向量生成矩阵:

v = [2 1 -1 -2 -5];

Use diag to create a matrix with the elements of v on the main diagonal.

D = diag(v)

D =

2 0 0 0 0

0 1 0 0 0

0 0 -1 0 0

0 0 0 -2 0

0 0 0 0 -5

Create a matrix with the elements of v on the first super diagonal (k=1).

D1 = diag(v,1)

D1 =

0 2 0 0 0 0

0 0 1 0 0 0

0 0 0 -1 0 0

0 0 0 0 -2 0

0 0 0 0 0 -5

0 0 0 0 0 0

The result is a 6-by-6 matrix. When you specify a vector of length n as an input, diag returns a square matrix of size n+abs(k).

对矩阵取对角线的值生成一个向量

A = randi(10,6)

A =

9 3 10 8 7 8

10 6 5 10 8 1

2 10 9 7 8 3

10 10 2 1 4 1

7 2 5 9 7 1

1 10 10 10 2 9

x = diag(A)

x =

9

6

9

1

7

9

Get the elements on the first subdiagonal (k=-1) of A. The result has one fewer element than the main diagonal.

x1 = diag(A,-1)

x1 =

10

10

2

9

2

Calling diag twice returns a diagonal matrix composed of the diagonal elements of the original matrix.

来自MATLAB help
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: