11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

0 00 0The second command illustrates how we canconstruct a matrix based on the size of an existingone. Try ones(size(D)).An n ⇥ n matrix that has the same numberof rows and columns and is called a squarematrix.A matrix is said to be symmetric if it is equalto its transpose (i.e. it is unchanged by transposition):>> S = [2 -1 0; -1 2 -1; 0 -1 2],S =2 -1 0-1 2 -10 -1 2>> St = S’St =2 -1 0-1 2 -10 -1 2>> S-Stans =0 0 00 0 00 0 015.4 The Identity MatrixThe n ⇥ n identity matrix is a matrix of zerosexcept for having ones along its leading diagonal(top left to bottom right). This is calledeye(n) in Matlab (since mathematically it isusually denoted by I).>> I = eye(3), x = [8; -4; 1], I*xI =1 0 00 1 00 0 1x =8-41ans =8-41Notice that multiplying the 3 ⇥ 1 vector x bythe 3 ⇥ 3 identity I has no e↵ect (it is like multiplyinga number by 1).15.5 Diagonal MatricesA diagonal matrix is similar to the identity matrixexcept that its diagonal entries are not necessarilyequal to 1.D =24 0 4 03 0 00 0 2is a 3 ⇥ 3 diagonal matrix. To construct this inMatlab, we could either type it in directly>> D = [-3 0 0; 0 4 0; 0 0 2]D =-3 0 00 4 00 0 2but this becomes impractical when the dimensionis large (e.g. a 100 ⇥ 100 diagonal matrix).We then use the diag function.We first define avector d, say, containing the values of the diagonalentries (in order) then diag(d) gives therequired matrix.>> d = [-3 4 2], D = diag(d)d =-3 4 2D =-3 0 00 4 00 0 2On the other hand, if A is any matrix, the commanddiag(A) extracts its diagonal entries:>> F = [0 1 8 7; 3 -2 -4 2; 4 2 1 1]F =0 1 8 73 -2 -4 24 2 1 1>> diag(F)ans =0-21Notice that the matrix does not have to besquare.3519

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

Saved successfully!

Ooh no, something went wrong!