* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download M2 Notes
Rotation matrix wikipedia , lookup
Principal component analysis wikipedia , lookup
Eigenvalues and eigenvectors wikipedia , lookup
Determinant wikipedia , lookup
Jordan normal form wikipedia , lookup
Singular-value decomposition wikipedia , lookup
Matrix (mathematics) wikipedia , lookup
Non-negative matrix factorization wikipedia , lookup
Perron–Frobenius theorem wikipedia , lookup
Orthogonal matrix wikipedia , lookup
Four-vector wikipedia , lookup
Cayley–Hamilton theorem wikipedia , lookup
Matrix calculus wikipedia , lookup
Introduction to Matlab Module #2 – Arrays • Topics 1. 2. 3. 4. • Numeric arrays (creation, addressing, sizes) Element-by-Element Operations Matrix Operations Polynomial Operations Textbook Reading Assignments 1. 2.1-2.5 • Practice Problems 1. Chapter 2, Problems: 1, 2, 3, 8, 9, 13 Introduction to Matlab Module #2 Page 1 1) Numeric Arrays • Arrays - An array is a collection of data that can be described with a single variable - Arrays are entered into Matlab using square brackets (i.e., y = [1, 2, 3]) • Row Vectors - A horizontal arrangement of elements: x 5 7 2 - Entered in Matlab using comma delimiters within the square brackets: >> x = [5, 7 2] - Individual elements are addressed using indexes starting at 1: x 5 7 2 1 2 x(1) → 5 x(2) → 7 x(3) → 2 3 Introduction to Matlab Module #2 Page 2 1) Numeric Arrays • Column Vectors - A vertical arrangement of elements 8 y 2 4 - Column entries are entered in Matlab using the semicolon within the square brackets >> y = [8; -2; 4] - Individual elements are addressed using indexes: 8 y 2 4 1 y(1) → 8 y(2) → -2 y(3) → 3 2 3 Introduction to Matlab Module #2 Page 3 1) Numeric Arrays • Automatic Creation of Arrays - Equally space elements can be created using: >> t = [0:1:10] start value step size end value - the same thing can be accomplished using the linspace(a,b,n) command, which allows you to enter the start (a), the end (b), and the number of elements in the array (n). Matlab will create the array with regular spacing between elements. >> t = linspace[0,10,11] start value end size # of points in array - logarithmic spacing can be accomplished using the logspace(a,b,n) command Introduction to Matlab Module #2 Page 4 1) Numeric Arrays • 2D Arrays or Matrices - Matrices are 2D arrays that are m x n in size where: n 2 5 A 3 4 7 1 m = # of rows n = # of columns m - Matrices can be directly entered in Matlab using a combination of commas and semicolons: >> A = [2,5; -3,4; -7,1] - When talking about Matrices, the row always comes first i.e., (row, column) - We say that A is a 3 x 2 matrix (i.e., it has 3 rows and 2 columns) Introduction to Matlab Module #2 Page 5 1) Numeric Arrays • Addressing Matrices - Individual entries can be addressed using the (row, column) location: 1 2 2 5 A 3 4 7 1 - The colon (:) represents all element addresses i.e., >> B = A(:,1) would yield: >> C = A(2,:) would yield A(1,1) → 2 A(1,2) → 5 A(2,1) → -3 A(2,2) → 4 1 A(3,1) → -7 A(3,2) → 1 2 3 2 B 3 7 C 3 4 Introduction to Matlab i.e., All rows, 1st column i.e., 2nd row, all columns Module #2 Page 6 1) Numeric Arrays • Addressing Matrices - A range of entries can be addressed using the semicolon: 1 2 2 5 A 3 4 7 1 i.e., 1 2 3 >> D = A(1:2,1:2) would yield: 2 5 D 3 4 i.e., rows 1 & 2, columns 1 & 2 Introduction to Matlab Module #2 Page 7 1) Numeric Arrays • Creating 2D Arrays with the Matrix Editor - A 2D array variable can be created using the empty or null operator [] D = [] this creates an empty matrix variable D - This variable can now be double clicked on in the Workspace to launch the Array editor Introduction to Matlab Module #2 Page 8 2) Array Operations • (element-by-element) A variety of built-in functions exist to help analyze a Matrix X 0 2 5 9 - length(M) 4 8 Y 6 7 2 5 Z 3 4 7 1 % if a vector, returns the number of elements in the array % if a matrix, returns the largest number of elements in the array (either row or column) >> length(X) → 4 >> length(Y) → 4 >> length(Z) → 3 Introduction to Matlab Module #2 Page 9 2) Array Operations • (element-by-element) A variety of built-in functions exist to help analyze a Matrix X 0 2 5 9 - size(M) 4 8 Y 6 7 2 5 Z 3 4 7 1 % returns the matrix size in row, column (m x n) format >> size(X) → [1 4] >> size(Y) → [4 1] >> size(Z) → [3 2] Introduction to Matlab Module #2 Page 10 2) Array Operations X 0 2 5 9 - max(M) 4 8 Y 6 7 2 5 Z 3 4 7 1 % if a vector, returns the algebraically largest value % if a matrix, returns the row vector containing the largest value >> max(X) >> max(Y) >> max(Z) - min(M) (element-by-element) → → → 9 8 [2 5] % if a vector, returns the algebraically smallest value % if a matrix, returns the row vector containing the smallest value >> min(X) >> min(Y) >> min(Z) → → → -5 -6 [-7 1] Introduction to Matlab Module #2 Page 11 2) Array Operations X 0 2 5 9 - sort(M) (element-by-element) 4 8 Y 6 7 2 5 Z 3 4 7 1 % if a row vector, returns a row vector of the same size elements in ascending order % if a column vector, returns a column vector of the same size elements in ascending order % if a matrix, returns a matrix of the same size with columns sorted in ascending order >> sort(X) → >> sort(Y) → >> sort(Z) → 5 0 2 9 6 4 7 8 7 1 3 4 2 5 Introduction to Matlab Module #2 Page 12 2) Array Operations X 0 2 5 9 (element-by-element) 4 8 Y 6 7 2 5 Z 3 4 7 1 - sum(M) % if a vector, returns a scalar with the sum of all elements % if a matrix, returns a row vector with the sum of each columns >> sum(X) → 6 >> sum(Y) → 5 >> sum(Z) → [-8 10] Introduction to Matlab Module #2 Page 13 2) Array Operations • Matrices can also be “Transposed” to interchange rows and columns X 0 2 5 9 0 2 T X 5 7 • (element-by-element) 4 8 Y 6 7 Y T 4 8 6 7 2 5 Z 3 4 7 1 2 3 7 ZT 5 4 1 The transpose command in Matlab is the tick (‘) >> X’ >> Y’ >> Z’ • The transpose command is often used to make the sort/sum commands work more efficiently Introduction to Matlab Module #2 Page 14 2) Array Operations • (element-by-element) Scalar-Array Operations - mathematical operations between a scalar and matrix are performed on each element within the matrix (element-by-element). - addition (+), subtraction (-), and multiplication (*) are performed on each element 2 4 6 A 8 10 12 ex) >> A+1 >> 1+A → → 3 5 7 9 11 13 >> A-1 → 1 3 5 7 9 11 >> A*2 >> 2*A → → 4 8 12 16 20 24 Introduction to Matlab Module #2 Page 15 2) Array Operations • (element-by-element) Scalar-Array Operations - division requires that the Array be the numerator - both left and right scalar division works 2 4 6 A 8 10 12 ex) >> A/2 >> 2\A → → 1 2 3 4 5 6 Introduction to Matlab Module #2 Page 16 2) Array Operations • Array Addition (element-by-element) (or element-by-element addition) - when performing mathematical operations with two matrix inputs, care needs to be taken to follow the rules of matrix algebra. - element-by-element operations are NOT always the same as traditional Matrix operations. - addition of two matrices requires that the two inputs be of the same size. The output is a matrix of the same size where each element is the sum of the two corresponding locations in the inputs. 2 4 6 A 8 10 12 ex) >> C = A + B 1 3 5 B 7 9 11 → ( A11 B11 ) ( A12 B12 ) ( A13 B13 ) (2 1) (4 3) (6 5) 3 7 11 C (8 7) (10 9) (12 11) 15 19 23 ( A B ) ( A B ) ( A B ) 21 22 22 23 23 21 Introduction to Matlab Module #2 Page 17 2) Array Operations • Array Subtraction (element-by-element) (or element-by-element subtraction) - subtraction of two matrices requires that the two inputs be of the same size. The output is a matrix of the same size where each element is the difference of the two corresponding locations in the inputs. 2 4 6 A 8 10 12 ex) >> C = A - B 1 3 5 B 7 9 11 → (6 5) 1 1 1 (a11 b11 ) (a12 b12 ) (a13 b13 ) (2 1) (4 3) C (8 7) (10 9) (12 11) 1 1 1 ( a b ) ( a b ) ( a b ) 22 22 23 23 21 21 Introduction to Matlab Module #2 Page 18 2) Array Operations • Array Multiplication (element-by-element) (or element-by-element multiplication) - The rules of traditional Matrix multiplication are different than element-by-element multiplication. - If you wish to perform element-by-element multiplication on two matrices of the same size, you use the operator (.*) 1 3 D 5 7 >> F = D .* E 2 4 E 6 8 → d11 d12 e11 e12 d11e11 d12e12 (1)( 2) (3)( 4) 2 12 F e d e (5)(6) (7)(8) 30 56 d d e d e 22 21 22 22 22 21 21 21 Introduction to Matlab Module #2 Page 19 2) Array Operations • Array Division (element-by-element) (or element-by-element multiplication) - The rules of traditional Matrix division are different than element-by-element division. - If you wish to perform element-by-element division on two matrices of the same size, you use the operator (./) or (.\) 1 3 D 5 7 >> F = D ./ E >> F = E .\ D 2 4 E 6 8 → → d11 d12 e11 e12 d11 / e11 d12 / e12 1 / 2 3 / 4 0.500 0.750 F / e d / e 5 / 6 7 / 8 0.833 0.875 d d e d / e 22 21 22 22 22 21 21 21 Introduction to Matlab Module #2 Page 20 2) Array Operations • Array Exponentiation (element-by-element) (or element-by-element exponentiation) - The rules of traditional Matrix exponentiation are different than element-by-element exponentiation. - If you wish to perform element-by-element exponentiation on a matrix you use the operator (.^) 2 4 6 A 8 10 12 >> F = A .^ 3 → 64 216 23 43 63 8 F 3 3 3 512 100 1728 8 10 12 Introduction to Matlab Module #2 Page 21 3) Matrix Operations • Matrix Multiplication (formal definition) - The rules of Matrix multiplication is that for C=AB: - that the number of columns in A is equal to the number or rows in B. - the product will be a matrix with the same # of rows in A and same # of columns in B i.e., a11 a12 C AB a21 a22 b1 a13 a11b1 a12b2 a13b3 b2 a23 a21b1 a22b2 a23b3 b3 - If the inputs to matrix-matrix multiplication follow these size rules, the multiplication operator is simply (*) 2 4 6 A 8 10 12 >> A*y → 8 y 2 4 (2)(8) (4)( 2) (6)( 4) 32 (8)(8) (10)( 2) (12)( 4) 92 Introduction to Matlab Module #2 Page 22 3) Matrix Operations • Matrix Division - The rules of Matrix division are more complicated and covered in Chapter 6 of the text. - The operators for Matrix division are (/) and (\) - There are a variety of conditions that must be met in order for this division to work properly (more later) Introduction to Matlab Module #2 Page 23 3) Matrix Operations • Matrix Exponentiation - Matrix exponentiation is defined as repeatedly multiplying the matrix by itself. i.e., A2 AA A3 AAA - This requires that the Matrix be a square (i.e., m = n) 1 2 S 3 4 >> S^2 → 7 10 15 22 Introduction to Matlab Notice this is NOT simply raising each element to a power of 2 Module #2 Page 24 4) Polynomial Operations • Polynomials - Polynomials are entered into Matlab using a Row Vector - Each of the entries in the row vector represents the coefficients of the terms in the polynomial - The coefficients are entered with the highest order on the left and the lowest on the right. Ex) 3x 3 x 2 10 x 5 would be entered as: >> [3 1 -10 5] - polynomial terms that don’t exists are entered with a coefficient of 0. 22 x 4 x Ex) would be entered as: >> [22 0 0 1 0] Introduction to Matlab Module #2 Page 25 4) Polynomial Operations • Polynomial Operations - Matlab has built in operations to evaluate polynomials: roots(a) % returns the roots of a polynomial poly(x) % computes the coefficients of a polynomial give the roots polyval(a,x) % evaluates the polynomial (a) at specified values of the % independent variable (x) Introduction to Matlab Module #2 Page 26 Lab 2 Exercise Problems - For each of these exercises, you will create a script file. Your script file will perform the calculations and then display the answers to the workspace. - Create a directory on your Z drive called “Z:\Matlab_Course\Lab02” - Change your pwd to “Z:\Matlab_Course\Lab02” (>> cd Z:\Matlab_Course\Lab02) - Perform the following exercises: 2.1 - Create a script file called Lab02_2d1a.m - Print a comment to the screen for each solution using the command disp 2.2 - Create a script file called Lab02_2d2.m 2.3 - Create a script file called Lab02_2d3.m 2.8 - Create a script file called Lab02_2d8.m 2.9 - Create a script file called Lab02_2d9.m 2.13 - Create a script file called Lab02_2d13.m Introduction to Matlab Module #2 Page 27