Function Reference
— Built-in Function: diag (v, k)

Return a diagonal matrix with vector v on diagonal k. The second argument is optional. If it is positive, the vector is placed on the k-th super-diagonal. If it is negative, it is placed on the -k-th sub-diagonal. The default value of k is 0, and the vector is placed on the main diagonal. For example,

          diag ([1, 2, 3], 1)
                0  1  0  0
                  0  0  2  0
                  0  0  0  3
                  0  0  0  0

Given a matrix argument, instead of a vector, diag extracts the k-th diagonal of the matrix.