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

22        Chapter 2: MATLAB Basics


                     To change the vector X from a row vector to a column vector, put a prime (’)
                     after X:


                       >> X’

                       ans =
                             0
                             2
                             4
                             6
                             8
                             10

                       You can perform mathematical operations on vectors. For example, to square
                     the elements of the vector X, type

                       >> X.ˆ2


                       ans =
                             0      4      16       36      64      100


                       The period in this expression is very important; it says that the numbers
                     in X should be squared individually, or element-by-element. Typing Xˆ2 would
                     tell MATLAB to use matrix multiplication to multiply X by itself and would
                     produce an error message in this case. (We discuss matrices below and in
                     Chapter 4.) Similarly, you must type .* or ./ if you want to multiply or di-
                     vide vectors element-by-element. For example, to multiply the elements of the
                     vector X by the corresponding elements of the vector Y, type


                       >> X.*Y

                       ans =
                             0      -6       20      -12       64      10

                       Most MATLAB operations are, by default, performed element-by-element.
                     For example, you do not type a period for addition and subtraction, and you
                     can type exp(X) to get the exponential of each number in X (the matrix ex-
                     ponential function is expm). One of the strengths of MATLAB is its ability to
                     efficiently perform operations on vectors.
   36   37   38   39   40   41   42   43   44   45   46