MATLAB Function Reference
  Go to function:
    Search    Help Desk 
diag    Examples   See Also

Diagonal matrices and diagonals of a matrix

Syntax

Description

X = diag(v,k) when v is a vector of n components, returns a square matrix X of order n+abs(k), with the elements of v on the kth diagonal. k = 0 represents the main diagonal, k > 0 above the main diagonal, and k < 0 below the main diagonal.


X = diag(v) puts v on the main diagonal, same as above with k = 0.

v = diag(X,k) for matrix X, returns a column vector v formed from the elements of the kth diagonal of X.

v = diag(X) returns the main diagonal of X, same as above with k = 0.

Examples

diag(diag(X)) is a diagonal matrix.

sum(diag(X)) is the trace of X.

The statement

produces a tridiagonal matrix of order 2*m+1.

See Also

spdiags, tril, triu



[ Previous | Help Desk | Next ]