Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays
Working with Arrays Chapter Outline Creating Vector and Matrix Accessing and Addressing Matrix Mathematical Operations with Matrix Functions for Analyzing Matrix Chapter Case Study
Working with Arrays Creating Vectors a n subdivision points b dx >> x = a:dx:b; >> x = linspace(a,b,n); >> x = x'; x row vectors convert to column vector
Working with Arrays Creating Vectors - Examples 1)Create a COLUMN VECTOR with the elements : 55, 14, log(51), 0, sin(pi/3) 2) Create a ROW VECTOR which the first element is 1, the last element is 33, with a increment of 2 between elements
Working with Arrays Creating Matrices >> A = [1,2,3; 4,5,6; 7,8,9]; >> A = [1 2 3; 4 5 6; 7 8 9]; >> A = [ ] >> A = A' or data entry mode transpose
Working with Arrays Array Operations A = B = C = A + B C = A = B = C = A.*B C = Array addition Array multiplication A = C = A.^2 C = Array power
Working with Arrays Array Multiplication Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices >> a = [ ; ]; >> b = [1:4; 1:4]; >> c = a.*b c = >> a = [ ; ]; >> b = [1:4; 1:4]; >> c = a.*b c = c(2,4) = a(2,4)*b(2,4)
Working with Arrays Matrix Operations A = B = 2*A B = A = B = 2 + A B = Scalar Multiplication Scalar Expansion A = B = C = A * B C = Matrix Multiplication
Working with Arrays >> e=[1 2;3 4] = = = 8 9 >> e=[1 2;3 4] = = = 8 9 Matrix Calculation-Scalar Expansion >> e=[1 2;3 4] + 5 e = >> e=[1 2;3 4] + 5 e = Scalar expansion
Working with Arrays Matrix Multiplication Inner dimensions must be equal. Dimension of resulting matrix = outermost dimensions of multiplied matrices. Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix. >> a = [1 2 3;4 5 6]; >> b = [3,1;2,4;-1,2]; >> c = a*b c = >> a = [1 2 3;4 5 6]; >> b = [3,1;2,4;-1,2]; >> c = a*b c = [2x3] [3x2] [2x3]*[3x2] [2x2] a(2nd row). b(2nd column)
Working with Arrays Array Addressing m(2:4,3) m(3,1)
Working with Arrays More Example on indexing >> a=[3 11 6; ; ] a = >> a(3,1)= 20 a = >> a(2,3)-a(1,2) ans = >> a=[3 11 6; ; ] a = >> a(3,1)= 20 a = >> a(2,3)-a(1,2) ans = Create a 3 x 3 matrix Use square brackets [ ] Matrices must be rectangular. (Undefined elements set to zero) Assign a new value to the (3,1) element Use elements in a mathematical expression
Working with Arrays More Example on Colon indexing >> a=[3 11 6; ; ] a = >> b = a(:,3) b = >> c = a(2,:) c = >> d = a(2:3,1:2) d = [4 7 ] [13 9 ] >> a=[3 11 6; ; ] a = >> b = a(:,3) b = >> c = a(2,:) c = >> d = a(2:3,1:2) d = [4 7 ] [13 9 ] Create a 3 x 3 matrix Define a column vector b from elements in all rows of column 3 in matrix a Define a row vector c from elements in all columns of row 2 in matrix a Create a matrix d from elements in rows 2&3 and columns 1&2 in matrix a
Working with Arrays Solve this set of simultaneous equations Array Division using “Left Division” >> A = [-1 1 2; ;-1 3 4]; >> b = [2;6;4]; >> x = inv(A)*b x = >> x = A\b x = >> A = [-1 1 2; ;-1 3 4]; >> b = [2;6;4]; >> x = inv(A)*b x = >> x = A\b x = x 1 + x 2 + 2x 3 = 2 3x 1 - x 2 + x 3 = 6 -x 1 + 3x 2 + 4x 3 = 4
Working with Arrays FunctionDescriptionExample C=max(A)If A is vector, C is the largest element in A A = [5 9 2] C = max(A) sum(A)If A is vector, returns the sum of elements of A A = [5 9 2] sum(A) sort(A)If A is vector, arranges elements of vector in ascending order A = [5 9 2] sort(A) det(A)Returns the determinant of a square matrix A A = [2 4; 3 5]; det (A) inv(A)Returns the inverse of a square matrix A A = [2 4; 3 5]; inv(A) Functions for Analyzing Matrix
Working with Arrays Sample Problem 2 : Friction Experiment The coefficient of friction, μ, can be determined in an experiment by measuring the force F required to move a mass m. When F is measured and m is known, the coefficient of friction can be calculated by: µ = F / (mg) where g = 9.81 m/s 2 Results from measuring F in six tests are given in the table below. Determine the coefficient of friction in each test, and the average from all tests. Test # Mass m (kg) Force F (N)
Working with Arrays Summary Creating Vector and Matrix Accessing and Addressing Matrix Mathematical Operations with Matrix Functions for Analyzing Matrix