Page 78 - A Guide to MATLAB for Beginners and Experienced Users
P. 78

More on Matrices         59


                     You can use MATLAB to do computations involving complex numbers by en-
                     tering numbers in the form a + b*i:


                       >> (2 + 3*i)*(4 - i)

                       ans =
                              11.0000 + 10.0000i

                       Complex arithmetic is a powerful and valuable feature. Even if you don’t in-
                     tend to use complex numbers, you should be alert to the possibility of complex-
                     valued answers when evaluating MATLAB expressions.




           More on Matrices


                     In addition to the usual algebraic methods of combining matrices (e.g., matrix
                     multiplication), we can also combine them element-wise. Specifically, if A and
                     B are the same size, then A.*B is the element-by-element product of A and B,
                     that is, the matrix whose i, j element is the product of the i, j elements of A
                     and B. Likewise, A./B is the element-by-element quotient of A and B, and A.ˆc
                     is the matrix formed by raising each of the elements of A to the power c. More
                     generally, if f is one of the built-in functions in MATLAB, or is a user-defined
                     function that accepts vector arguments, then f(A) is the matrix obtained
                     by applying f element-by-element to A. See what happens when you type
                     sqrt(A), where A is the matrix defined at the beginning of the Matrices
                     section of Chapter 2.
                       Recall that x(3) is the third element of a vector x. Likewise, A(2,3) rep-
                     resents the 2, 3 element of A, that is, the element in the second row and third
                     column. You can specify submatrices in a similar way. Typing A(2,[2 4])
                     yields the second and fourth elements of the second row of A. To select the
                     second, third, and fourth elements of this row, type A(2,2:4). The subma-
                     trix consisting of the elements in rows 2 and 3 and in columns 2, 3, and 4 is
                     generated by A(2:3,2:4). A colon by itself denotes an entire row or column.
                     For example, A(:,2) denotes the second column of A, and A(3,:) yields the
                     third row of A.
                       MATLAB has several commands that generate special matrices. The com-
                     mands zeros(n,m) and ones(n,m) produce n × mmatrices of zeros and ones,
                     respectively. Also, eye(n) represents the n × n identity matrix.
   73   74   75   76   77   78   79   80   81   82   83