Page 24 - MATLAB Recipes for Earth Sciences
P. 24

2.3 The Syntax                                                   15

           2.3 The Syntax

           The name MATLAB stands for matrix laboratory. The classic object handled
           by MATLAB is a  matrix, i.e., a rectangular two-dimensional  array of num-
           bers. A simple 1-by-1 matrix is a  scalar. Matrices with one  column or  row
           are  vectors,  time series and other one-dimensional data fi elds. An m-by-n
           matrix can be used for a digital elevation model or a grayscale image. RGB
           color images are usually stored as three-dimensional arrays, i.e., the colors
           red, green and blue are represented by a m-by-n-by-3 array.
             Entering matrices in MATLAB is easy. To enter an arbitrary matrix, type
             A = [2 4 3 7; 9 3 -1 2; 1 9 3 7; 6 6 3 -2]

           after the prompt, which fi rst defines a variable A, then lists the elements of

           the matrix in  square brackets. The rows of A are separated by  semicolons,
           whereas the elements of a row are separated by  blanks, or, alternatively, by
             commas. After pressing  return, MATLAB displays the matrix
             A =
                 2  4  3  7
                 9  3 -1  2
                 1  9  3  7
                 6  6  3 -2

           Displaying the elements of A could be problematic in case of very large ma-
           trices, such as digital elevation models consisting of thousands or millions
           of elements. In order to suppress the display of a matrix or the result of an
           operation in general, you should end the line with a semicolon.

             A = [2 4 3 7; 9 3 -1 2; 1 9 3 7; 6 6 3 -2];
           The matrix A is now stored in the  workspace and we can do some basic op-
           erations with it, such as computing the  sum of elements,
             sum(A)

           which results in the display of

             ans =
                 18  22  8  14

           Since we did not specify an output variable, such as A for the matrix entered
           above, MATLAB uses a default variable   ans, short for answer, to store the
           results of the calculation. In general, we should define variables since the

           next computation without a new variable name overwrites the contents of
           ans.
   19   20   21   22   23   24   25   26   27   28   29