Matlab Training Session 2: Matrix Operations and Relational Operators

Slides:



Advertisements
Similar presentations
3_3 An Useful Overview of Matrix Algebra
Advertisements

Review of Matrix Algebra
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Matrix Mathematics in MATLAB and Excel
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 10 Review: Matrix Algebra
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7.1 Solving Systems of Two Equations.
ECON 1150 Matrix Operations Special Matrices
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Matrix Algebra. Quick Review Quick Review Solutions.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Copyright © 2011 Pearson, Inc. 7.2 Matrix Algebra.
Lecture 28: Mathematical Insight and Engineering.
Unit 3: Matrices.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
3.4 Solution by Matrices. What is a Matrix? matrix A matrix is a rectangular array of numbers.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
MTH108 Business Math I Lecture 20.
ECE 1304 Introduction to Electrical and Computer Engineering
Sections 2.4 and 2.5 Matrix Operations
Linear Algebraic Equations and Matrices
Introduction to MATLAB for Engineers, Third Edition
Boyce/DiPrima 10th ed, Ch 7.2: Review of Matrices Elementary Differential Equations and Boundary Value Problems, 10th edition, by William E. Boyce and.
Linear Algebra Lecture 2.
Chapter 7 Matrix Mathematics
ECON 213 Elements of Mathematics for Economists
Matlab Training Session 4: Control, Flow and Functions
L – Modeling and Simulating Social Systems with MATLAB
Linear Algebraic Equations and Matrices
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
EEE 244 Numerical Methods In Electrical Engineering
StatLab Matlab Workshop
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
MATH 493 Introduction to MATLAB
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Vectors and Matrices I.
Lecture 11 Matrices and Linear Algebra with MATLAB
Introduction to MATLAB [Vectors and Matrices] Lab 2
Matlab Training Session 5: Importing Data
Unit 3: Matrices
2.2 Introduction to Matrices
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
MATLAB Programming Basics Copyright © Software Carpentry 2011
Basics of Linear Algebra
Matrices in MATLAB Dr. Risanuri Hidayat.
EECS Introduction to Computing for the Physical Sciences
Laboratory in Oceanography: Data and Methods
Matrix Operations Ms. Olifer.
Matrices and Determinants
Announcements.
Presentation transcript:

Matlab Training Session 2: Matrix Operations and Relational Operators

Course Outline Weeks: Introduction to Matlab and its Interface (Jan 13 2009) 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.php

Fundamentals of Matlab 1 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

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

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: >> A = [1 2 4 5;6 3 8 2] A is a 2 x 4 matrix >> A(2,1) Ans 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] >> A(2,1) = 9 Ans 9 3 8 2]

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 >> A(1,1:3) Ans 1 2 4

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 >> A(:,3) Ans 4 8 >> A(2,:) Ans 6 3 8 2

B. Matrix Operations

Matrix Operations Indexing Matrices An empty or null matrix can be created using square brackets >> A = [ ] ** TIP: The size and length functions can quickly return the number of elements and dimensions of a matrix variable

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 >> A(:,3) = [ ] A = [1 2 5 6 3 2] >> A(2,:) = [ ] A = [1 2 5]

Matrix Operations Indexing Matrices A = [1 2 4 5 6 3 8 2] However individual elements within a matrix cannot be assigned an empty value >> A(1,3) = [ ] ??? Subscripted assignment dimension mismatch.

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)

N – Dimensional Matrices Examples A = [1 2 4 5 B = [5 3 7 9 6 3 8 2] 1 9 9 8] >> C = cat(3,[1,2,4,5;6,3,8,2],[5,3,7,9;1,9,9,8]) >> C = cat(3,A,B)

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

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; A.^C; A*B

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 A^C What is happening here?

Matrix Operations Matrix Operations Addition and Subtraction 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: >>A + B >>A + C >>A + D

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

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)

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)

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)

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)

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: >>A * D >>B * E >>A * B

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] >>A .* D Ans = [ 2 4 12 6]

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

Matrix Operations Left and Right Division Left and Right division utilizes the / and \ operators Left (\) division: X = A\B is a solution to A*X = B Right (/) division: X = B/A is a solution to X*A = 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

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] >>A ./ D Ans = [ 0.5000 1.0000 2.0000 2.5000 3.0000 1.5000 4.0000 1.0000 ]

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

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

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] >> transpose(A) >> B’ A = [1 6 B = [1 7 3 3] 2 3 4 8 5 2]

Matrix Operations Other handy built in matrix functions Include: inv() Matrix inverse det() Matrix determinant poly() Characteristic Polynomial kron() Kronecker tensor product

C. Relational Operators

Relational Operators Relational Operators < less than Relational operators are used to compare two scaler values or matrices of equal dimensions Relational Operators < less than <= less than or equal to > Greater than >= Greater than or equal to == equal ~= not equal

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! >> 5 == 5 Ans 1 >> 20 >= 15

Relational Operators A = [1 2 4 5 B = 7 C = [2 2 2 2 6 3 8 2] 2 2 2 2] 6 3 8 2] 2 2 2 2] Try: >>A > B >> A < C

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 >> find(D > 0) The resultant positions can be used as indexes to change these elements >> D(find(D>0)) = 10 D = [10 2 10 5 10 2]

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 >> [x,y] = find(A == 5) The matching elements will be indexed by (x1,y1), (x2,y2), … >> A(x,y) = 10 A = [ 1 2 4 10 6 3 8 2 ]

Getting Help Help and Documentation Digital Hard Copy Accessible Help from the Matlab Start Menu Updated online help from the Matlab Mathworks website: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html Matlab command prompt function lookup Built in Demo’s Websites Hard Copy Books, Guides, Reference The Student Edition of Matlab pub. Mathworks Inc.