23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

1 Data Structures<br />

OperatingonDiagonalMatrices<br />

There are several <strong>MATLAB</strong> functions that work specifically on diagonal<br />

matrices.<br />

Function<br />

blkdiag<br />

diag<br />

trace<br />

tril<br />

triu<br />

Description<br />

Construct a block diagonal matrix from input arguments.<br />

Return a diagonal matrix or the diagonals of a matrix.<br />

Compute the sum of the elements on the main diagonal.<br />

Return the lower triangularpartofamatrix.<br />

Return the upper triangular part of a matrix.<br />

This section covers the following topics on diagonal matrices:<br />

• “Constructing a Matrix from a Diagonal Vector” on page 1-42<br />

• “Returning a Triangular Portion of a Matrix” on page 1-43<br />

• “Concatenating Matrices Diagonally” on page 1-43<br />

Constructing a Matrix from a Diagonal Vector<br />

The diag function has two operations that it can perform. You can use it to<br />

generate a diagonal matrix:<br />

A = diag([12:4:32])<br />

A =<br />

12 0 0 0 0 0<br />

0 16 0 0 0 0<br />

0 0 20 0 0 0<br />

0 0 0 24 0 0<br />

0 0 0 0 28 0<br />

0 0 0 0 0 32<br />

You can also use the diag function to scan an existing matrix and return the<br />

values found along one of the diagonals:<br />

A = magic(5)<br />

A =<br />

1-42

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

Saved successfully!

Ooh no, something went wrong!