DIAG Create or extract diagonals. If V is a row or column vector with N components, DIAG(V,K) is a square matrix of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal. DIAG(V) simply puts V on the main diagonal. For example, DIAG(-M:M) + DIAG(ONES(2*M,1),1) + DIAG(ONES(2*M,1),-1) produces a tridiagonal matrix of order 2*M+1. If X is a matrix, DIAG(X,K) is a column vector formed from the elements of the K-th diagonal of X. DIAG(X) is the main diagonal of X. DIAG(DIAG(X)) is a diagonal matrix.