matlab diag 函数

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值