X = diag(v,k)
以向量v的元素作为矩阵X的第k条对角线元素,当k=0时,v为X的主对角线;当k>0时,v为上方第k条对角线
几个例子:
当k> v=[1 2 3];
>> x=diag(v,-1)
x =
0 0 0 0
1 0 0 0
0 2 0 0
0 0 3 0
v=[1];
x=diag(v,0)
x=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> clear
>> v(1)=1;
>> n=5;
>> v(2:n)=2;
>> x=diag(v,-1)
x =
0 0 0 0 0 0
1 0 0 0 0 0
0 2 0 0 0 0
0 0 2 0 0 0
0 0 0 2 0 0
0 0 0 0 2 0