diag
Diagonal of a matrix or a matrix with a given diagobal.
z=diag(x)
z=diag(x,k)
Inputs
x a vector or a square matrix.
k an integer whose absolute value is less than the size of z . Optional input whose defaut value is zero.
Outputs
z a matrix or a vector depending on the size of x .


Description
If v is a vector , diag(v, k) returns a square matrix whose k'th sub diagonal is v . If v is a matrix, the function returns the k'th sub-diagonal of v .


Example
>>a=[1 2 3 ; 4 5 6; 7 8 9]
>>a
          1          2          3
          4          5          6
          7          8          9

>>diag(a)
          1
          5
          9

>>diag(a,1)
          2
          6

>>diag(a,-1)
          4
          8

>>diag(b=[2 3 4])
          2          0          0
          0          3          0
          0          0          4

>>diag(b,1)
          0          2          0          0
          0          0          3          0
          0          0          0          4
          0          0          0          0

>>diag(b,-1)
          0          0          0          0
          2          0          0          0
          0          3          0          0
          0          0          4          0