An interactive introduction to MATLAB

(Jeff_L) #1

12 basic concepts


Comments:


  • On Line 7 thesizecommand returns the number of rows and columns
    in the matrix.

  • On Lines 11, 15 and 19, when accessing an individual element in a matrix,
    the first number after the round bracket refers to the row number (row
    index), and second number refers to the column number (column index).

  • On Line 19 the colon operator is used to denote all of the columns, i.e.
    all the columns in the third row are selected. The colon operator can also
    be used as a row index to denote all rows.

  • Line 23 demonstrates accessing a single element in the matrixwto change
    its value.

  • On Line 29 a new matrixvis created as a sub-matrix ofw.

  • Finally, on Line 34 a new matrixzis created as a sub-matrix ofw. Square
    brackets are used within the round brackets to enclose the list of row and
    column numbers.


Indexing arrays


(http://www.eng.ed.ac.uk/teaching/courses/matlab/unit01/indexing-
arrays.shtml)

Self Test Exercise: Indexing arrays


  1. †The following matrix is defined:


M=









6 9 12 15 18 21

4 4 4 4 4 4

2 1 0 − 1 − 2 − 3

− 6 − 4 −2 0 2 4









Evaluate the following expressions without usingMATLAB. Check your
answers withMATLAB.
a)A = M([1,3], [2,4])
b) B = M(:, [1,4:6])
c)C = M([2,3], :)
†Question adapted from Gilat, A. (2008).MATLAB : An Introduction With Applications. John
Wiley & Sons, Inc., 3rd edition. Copyright©2008 John Wiley & Sons, Inc. and reprinted
with permission of John Wiley & Sons, Inc.
Free download pdf