Go to CCP Homepage Go to Materials Page Go to Linear Algebra Materials Go to Table of Contents
Go Back One Page Go Forward One Page

MATLAB Tutor

Part 8: Matrices

  1. To enter a matrix in MATLAB, use square brackets. Enter

         [1 0 5
          2 8 7
          4 4 9]


    Just leave spaces between the row entries, and press Enter at the end of each row.
  2. To give your matrix a name, enter

         A = ans
  3. Enter

         M = [2 2  3 4
                 1 0 -9 5]
  4. Enter

         format rational
         H = hilb(9)

The Hilbert matrix is a certain kind of matrix. You do not need to know its definition right now. The important thing to note is that, if a matrix is too big to fit on the screen, MATLAB will break it up into smaller sections for display.

  1. Enter

         A = hilb(5)
  2. Enter

         A(2,3)
  3. Enter

         A(4, : )
  4. Enter

         A( : , 1)

Note that MATLAB uses the colon as a "wild card." A(4, : ) means "A (4, anything)," or the elements of matrix A, in row 4, and all the columns – so you get all of row 4 of A. Similarly, A(:,1) represents column 1 of A.

  1. Ask MATLAB to display row 3 of A, then display column 5. Display only the entry in row 5, column 5.
  2. Enter

         2 : 7

    and write a comment to describe what happens.
  3. Enter

         2 : 3 : 8

    and describe the result.
  4. Enter

         1 : 2 : 10

    Did you get what you expected?
  5. You can use this technique to display columns 2 through 4 of matrix A, for instance. Enter

         A ( : , 2:4)
  6. Enter a command that will display the first three rows of A.
  7. Enter

         R = [2 3 6
                 4 4 4]


    Enter

         S = [1 0 0
                 0 1 0]


    Enter

         T = [4 -1
                 2  1
                 0  5]
  8. Enter

         R'     % Transpose

    Enter

         T'

  9. Enter

         2 * R

    and describe what happens.
  10. Enter

         2R

    and describe what happens.
  11. Enter

         R + S
  12. Enter

         R - S
  13. Enter

         S*T

    Enter

         T*S

    Enter

         ST

    Remember that you must always use an asterisk to indicate multiplication – of one real number by another, or of a matrix by a constant, or of a matrix by another matrix.
  14. Enter

         R*S

    What happens?
  15. Enter

         U = [1 2 3; 0 2 3; 0 0 3]

    and note what happens. You may use this shortcut method for entering matrices if you like.
  16. Enter

         I_4 = eye(4)
         I_6 = eye(6)
         Z_2 = zeros(2)
         Z_5 = zeros(5)


    These matrix definitions are built into MATLAB; you may use them whenever you need these special matrices.

Go to CCP Homepage Go to Materials Page Go to Linear Algebra Materials Go to Table of Contents
Go Back One Page Go Forward One Page


| CCP Home | Materials | Linear Algebra | Module Contents | Back | Forward |

modules at math.duke.edu Copyright CCP and the author(s), 2000