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

Vectors and Matrices        21


           Vectors
                     A vector is an ordered list of numbers. You can enter a vector of any lengthin
                     MATLAB by typing a list of numbers, separated by commas or spaces, inside
                     square brackets. For example,

                       >> Z = [2,4,6,8]


                       Z=
                             2      4      6      8

                       >>Y=[4-35-281]


                       Y=
                             4       -3       5      -2      8      1

                       Suppose you want to create a vector of values running from 1 to 9. Here’s
                     how to do it without typing each number:

                       >> X = 1:9


                       X=
                             1      2      3      4       5      6      7      8      9

                     The notation 1:9 is used to represent a vector of numbers running from 1 to
                     9 in increments of 1. The increment can be specified as the second of three
                     arguments:

                       >> X = 0:2:10


                       X=
                             0      2      4      6       8      10

                     You can also use fractional or negative increments, for example, 0:0.1:1 or
                     100:-1:0.
                       The elements of the vector X can be extracted as X(1), X(2), etc. For ex-
                     ample,

                       >> X(3)

                       ans =
                             4
   35   36   37   38   39   40   41   42   43   44   45