Page 19 - Basics of MATLAB and Beyond
P. 19

a=[123;4 56;789]
                               matlab responds with

                               a  =
                                    1     2      3
                                    4     5      6
                                    7     8      9

                               3.2   Concatenating Matrices

                               Matrices can be made up of submatrices: Try this:

                               >>b=[a 10*a;-a [1 0 0;0 1 0;0 0 1]]
                               b  =
                                    1     2      3    10    20     30
                                    4     5      6    40    50     60
                                    7     8      9    70    80     90
                                   -1    -2     -3     1     0      0
                                   -4    -5     -6     0     1      0
                                   -7    -8     -9     0     0      1
                                  The repmat function can be used to replicate a matrix:

                               >>a=[12;34]
                               a  =
                                    1     2
                                    3     4
                               >> repmat(a,2,3)
                               ans  =
                                    1     2      1     2     1      2
                                    3     4      3     4     3      4
                                    1     2      1     2     1      2
                                    3     4      3     4     3      4


                               3.3   Useful Matrix Generators
                               matlab provides four easy ways to generate certain simple matrices.
                               These are
                                   zeros   a matrix filled with zeros
                                   ones    a matrix filled with ones
                                   rand    a matrix with uniformly distributed random elements
                                   randn   a matrix with normally distributed random elements
                                   eye     identity matrix
                               To tell matlab how big these matrices should be you give the functions
                               the number of rows and columns. For example:



                               c   2000 by CRC Press LLC
   14   15   16   17   18   19   20   21   22   23   24