Page 26 - MATLAB Recipes for Earth Sciences
P. 26

2.3 The Syntax                                                   17

           If you wish to replace several elements at one time, you can use the colon
           operator. Typing

             A(3,1:4) = [1 3 3 5];
           replaces all elements of the third row of matrix A. The  colon operator is used
           for other several things in MATLAB, for instance as an abbreviation for
           entering matrix elements such as
             c = 0 : 10

           which creates a row vector containing all integers from 0 to 10. The corre-
           sponding MATLAB response is

             c =
                 0 1 2 3 4 5 6 7 8 9 10

           Note that this statement creates 11 elements, i.e., the integers from 1 to 10
           and the zero. A common error while   indexing matrices is the ignorance of
           the zero and therefore expecting 10 instead of 11 elements in our example.
           We can  check this from the output of   whos.
             Name      Size                    Bytes  Class
             A         4x4                       128  double array
             b         1x1                         8  double array
             c         1x11                       88  double array
             Grand total is 28 elements using 224 bytes

           The above command only creates integers, i.e., the interval between the
           vector elements is one. However, an arbitrary interval can be defi ned, for
           example 0.5. This is later used to create evenly-spaced time axes for time
           series analysis for instance.
             c = 1 : 0.5 : 10;
             c =
               Columns 1 through 6
                 1.0000    1.5000    2.0000    2.5000    3.0000    3.5000
               Columns 7 through 12
                 4.0000    4.5000    5.0000    5.5000    6.0000    6.5000
               Columns 13 through 18
                 7.0000    7.5000    8.0000    8.5000    9.0000    9.5000
               Column 19
                10.0000

           The display of the values of a variable can be interrupted by pressing Ctrl-C
           (Control-C) on the keyboard. This interruption only affects the output in
           the Command Window, whereas the actual command is processed before
           displaying the result.
   21   22   23   24   25   26   27   28   29   30   31