23.12.2012 Views

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

MATLAB Function Reference Volume 1: A - E - Bad Request

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2diag<br />

Purpose Diagonal matrices and diagonals of a matrix<br />

Syntax X = diag(v,k)<br />

X = diag(v)<br />

v = diag(X,k)<br />

v = diag(X)<br />

Description X = diag(v,k) when v is a vector of n components, returns a square matrix X<br />

of order n+abs(k), with the elements of v on the kth diagonal. k = 0represents<br />

the main diagonal, k > 0 above the main diagonal, and k < 0 below the main<br />

diagonal.<br />

k = 0<br />

k < 0<br />

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

v = diag(X,k) for matrix X, returns a column vector v formed from the<br />

elements of the kth diagonal of X.<br />

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

Examples diag(diag(X)) is a diagonal matrix.<br />

sum(diag(X)) is the trace of X.<br />

The statement<br />

k > 0<br />

diag(-m:m)+diag(ones(2*m,1),1)+diag(ones(2*m,1),-1)<br />

produces a tridiagonal matrix of order 2*m+1.<br />

diag<br />

2-439

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!