Matlab Training Session 2: Matrix Operations and Relational Operators PowerPoint PPT Presentation

presentation player overlay
1 / 38
About This Presentation
Transcript and Presenter's Notes

Title: Matlab Training Session 2: Matrix Operations and Relational Operators


1
Matlab Training Session 2Matrix Operations and
Relational Operators
2
  • Course Outline
  • Weeks
  • Introduction to Matlab and its Interface (Oct. 23
    2006)
  • Fundamentals (Operators)
  • Fundamentals (Flow)
  • Importing Data
  • Functions and M-Files
  • Plotting (2D and 3D)
  • Statistical Tools in Matlab
  • Analysis and Data Structures
  • Course Website
  • http//www.queensu.ca/neurosci/Matlab Training
    Sessions.htm

3
  • Week 2 Lecture Outline
  • Fundamentals of Matlab 1
  • Week 1 Review
  • B. Matrix Operations
  • The empty matrix
  • Creating multi-dimensional matrices
  • Manipulating Matrices
  • Matrix Operations
  • C. Operators
  • Relational Operators

4
Week 1 Review
Working with Matrices c 5.66 or c 5.66
c is a scalar or a 1 x 1 matrix  x
3.5, 33.22, 24.5 x is a row vector or a
1 x 3 matrix  x1 2 5 3
-1 x1 is
column vector or a 4 x 1 matrix A 1 2 4
2 -2 2 0 3 5 5 4 9
A is a 4 x 3 matrix
5
Week 1 Review
  • Indexing Matrices
  • A m x n matrix is defined by the number of m rows
    and number of n columns
  • An individual element of a matrix can be
    specified with the notation A(i,j) or Ai,j for
    the generalized element, or by A(4,1)5 for a
    specific element.
  • Example
  • gtgt A 1 2 4 56 3 8 2 A is a
    4 x 2 matrix
  • gtgt A(1,2)
  • Ans 6

6
Week 1 Review
  • Indexing Matrices
  • Specific elements of any matrix can be
    overwritten using the matrix index
  • Example
  • A 1 2 4 5
  • 6 3 8 2
  • gtgt A(1,2) 9
  • Ans
  • A 1 2 4 5
  • 9 3 8 2

7
Week 1 Review
  • Indexing Matrices
  • A 1 2 4 5
  • 6 3 8 2
  • The colon operator can be used to index a range
    of elements
  • gtgt A(13,2)
  • Ans 1 2 4

8
Matrix Indexing Cont..
  • Indexing Matrices
  • A 1 2 4 5
  • 6 3 8 2
  • The colon operator can index all rows or columns
    without setting an explicit range
  • gtgt A(,3)
  • Ans 4 8
  • gtgt A(2,)
  • Ans 6 3 8 2

9
B. Matrix Operations
10
Matrix Operations
  • Indexing Matrices
  • An empty or null matrix can be created using
    square brackets
  • gtgt A
  • TIP The size and length functions can quickly
    return the number of elements and dimensions of a
    matrix variable

11
Matrix Operations
  • Indexing Matrices
  • A 1 2 4 5
  • 6 3 8 2
  • The colon operator can can be used to remove
    entire rows or columns
  • gtgt A(,3)
  • A 1 2 5
  • 6 3 2
  • gtgt A(2,)
  • A 1 2 5

12
N Dimensional Matrices
  • A 1 2 4 5 B 5 3 7 9
  • 6 3 8 2 1 9 9 8
  • Multidimensional matrices can be created by
    concatenating 2-D matrices together
  • The cat function concatenates matrices of
    compatible dimensions together
  • Usage cat(dimensions, Matrix1, Matrix2)

13
N Dimensional Matrices
  • Examples
  • A 1 2 4 5 B 5 3 7 9
  • 6 3 8 2 1 9 9 8
  • gtgt C cat(3,1,2,4,56,3,8,2,5,3,7,91,9,9,8)
  • gtgt C cat(3,A,B)

14
Matrix Operations
  • Scalar Operations
  • Scalar (single value) calculations can be can
    performed on matrices and arrays
  • Basic Calculation Operators
  • Addition
  • - Subtraction
  • Multiplication
  • / Division
  • Exponentiation

15
Matrix Operations
  • Scalar Operations
  • Scalar (single value) calculations can be
    performed on matrices and arrays
  • A 1 2 4 5 B 1 C 5
  • 6 3 8 2 7
  • 3
  • 3
  • Try
  • A 10
  • A 5
  • B / 2
  • AC

16
Matrix Operations
  • Scalar Operations
  • Scalar (single value) calculations can be
    performed on matrices and arrays
  • A 1 2 4 5 B 1 C 5
  • 6 3 8 2 7
  • 3
  • 3
  • Try
  • A 10
  • A 5
  • B / 2
  • AC What is happening here?

17
Matrix Operations
  • Matrix Operations
  • Matrix to matrix calculations can be performed on
    matrices and arrays
  • Addition and Subtraction
  • Matrix dimensions must be the same or the
    added/subtracted value must be scalar
  • A 1 2 4 5 B 1 C 5 D 2 4
    6 8
  • 6 3 8 2 7
    1 3 5 7
  • 3
  • 3
  • Try
  • gtgtA B gtgtA C gtgtA D

18
Matrix Operations
  • Matrix Multiplication
  • Built in matrix multiplication in Matlab is
    either
  • Algebraic dot product
  • Element by element multiplication

19
Matrix Operations
  • The Dot Product
  • The dot product for two matrices A and B is
    defined whenever the number of columns of A are
    equal to the number of rows of b
  • A(x1,y1) B(x2,y2)

20
Matrix Operations
  • The Dot Product
  • The dot product for two matrices A and B is
    defined whenever the number of columns of A are
    equal to the number of rows of b
  • A(x1,y1) B(x2,y2)

21
Matrix Operations
  • The Dot Product
  • The dot product for two matrices A and B is
    defined whenever the number of columns of A are
    equal to the number of rows of b
  • A(x1,y1) B(x2,y2)

22
Matrix Operations
  • The Dot Product
  • The dot product for two matrices A and B is
    defined whenever the number of columns of A are
    equal to the number of rows of b
  • A(x1,y1) B(x2,y2) C(x1,y2)

23
Matrix Operations
  • The Dot Product
  • A(x1,y1) B(x2,y2) C(x1,y2)
  • A 1 2 B 1 D 2 2 E 2 4
    3 6
  • 6 3 7 2 2
  • 3
  • 3
  • Try
  • gtgtA D
  • gtgtB E
  • gtgtA B

24
Matrix Operations
  • Element by Element Multiplication
  • Element by element multiplication of matrices is
    performed with the . operator
  • Matrices must have identical dimensions
  • A 1 2 B 1 D 2 2 E 2
    4 3 6
  • 6 3 7 2 2
  • 3
  • 3
  • gtgtA . D
  • Ans 2 4
  • 12 6

25
Matrix Operations
  • Matrix Division
  • Built in matrix division in Matlab is either
  • Left or right matrix division
  • Element by element division

26
Matrix Operations
  • Left and Right Division
  • Left and Right division utilizes the / and \
    operators
  • Left (\) division
  • X A\B is a solution to AX B
  • Right (/) division
  • X B/A is a solution to XA B
  • Left division requires A and B have the same
    number of rows
  • Right division requires A and B have the same
    number of columns

27
Matrix Operations
  • Element by Element Division
  • Element by element division of matrices is
    performed with the ./ operator
  • Matrices must have identical dimensions
  • A 1 2 4 5 B 1 D 2 2 2 2 E
    2 4 3 6
  • 6 3 8 2 7 2
    2 2 2
  • 3
  • 3
  • gtgtA ./ D
  • Ans 0.5000 1.0000 2.0000 2.5000
  • 3.0000 1.5000 4.0000 1.0000

28
Matrix Operations
  • Element by Element Division
  • Any division by zero will be returned as a NAN in
    matlab (not a number)
  • Any subsequent operation with a NAN value will
    return NAN

29
Matrix Operations
  • Matrix Exponents
  • Built in matrix Exponentiation in Matlab is
    either
  • A series of Algebraic dot products
  • Element by element exponentiation
  • Examples
  • A2 A A (Matrix must be square)
  • A.2 A . A

30
Matrix Operations
  • Shortcut Transposing Matrices
  • The transpose of a matrix is the matrix formed by
    interchanging the rows and columns of a given
    matrix
  • A 1 2 4 5 B 1
  • 6 3 8 2 7
  • 3
  • 3
  • gtgt transpose(A) gtgt
    B
  • A 1 6
    B 1 7 3 3
  • 2 3
  • 4 8
  • 5 2

31
Matrix Operations
Other handy built in matrix functions
Include inv() Matrix inverse det()
Matrix determinant poly() Characteristic
Polynomial kron() Kronecker tensor product
32
C. Relational Operators
33
Relational Operators
  • Relational operators are used to compare two
    scaler values or matrices of equal dimensions
  • Relational Operators
  • lt less than
  • lt less than or equal to
  • gt Greater than
  • gt Greater than or equal to
  • equal
  • not equal

34
Relational Operators
  • Comparison occurs between pairs of corresponding
    elements
  • A 1 or 0 is returned for each comparison
    indicating TRUE or FALSE
  • Matrix dimensions must be equal!
  • gtgt 5 5
  • Ans 1
  • gtgt 20 gt 15
  • Ans 1

35
Relational Operators
A 1 2 4 5 B 7 C 2 2 2 2
6 3 8 2 2 2 2
2 Try gtgtA gt B gtgt A lt C

36
Relational Operators
  • The Find Function
  • The find function is extremely helpful with
    relational operators for finding all matrix
    elements that fit some criteria
  • A 1 2 4 5 B 7 C 2 2 2 2
    D 0 2 0 5 0 2
  • 6 3 8 2 2
    2 2 2
  • The positions of all elements that fit the given
    criteria are returned
  • gtgt find(D gt 0)
  • The resultant positions can be used as indexes to
    change these elements
  • gtgt D(find(Dgt0)) 10 D 10 2
    10 5 10 2

37
Relational Operators
  • The Find Function
  • A 1 2 4 5 B 7 C 2 2 2 2
    D 0 2 0 5 0 2
  • 6 3 8 2 2
    2 2 2
  • The find function can also return the row and
    column indexes of of matching elements by
    specifying row and column arguments
  • gtgt x,y find(A 5)
  • The matching elements will be indexed by (x1,y1),
    (x2,y2),
  • gtgt A(x,y) 10
  • A 1 2 4 10
  • 6 3 8 2

38
Getting Help
  • Help and Documentation
  • Digital
  • Accessible Help from the Matlab Start Menu
  • Updated online help from the Matlab Mathworks
    website
  • http//www.mathworks.com/access/helpdesk/help/tech
    doc/matlab.html
  • Matlab command prompt function lookup
  • Built in Demos
  • Websites
  • Hard Copy
  • Books, Guides, Reference
  • The Student Edition of Matlab pub. Mathworks Inc.
Write a Comment
User Comments (0)
About PowerShow.com