CSE123 Lecture 5 Arrays and Array Operations Definitions • Scalars: Variables that represent single numbers. Note that complex numbers are also scalars, even though they have two components. • Arrays: Variables that represent more than one number. Each number is called an element of the array. Array operations allow operating on multiple numbers at once. • Row and Column Arrays (Vector): A row of numbers (called a row vector) or a column of numbers(called a column vector). • Two-Dimensional Arrays (Matrix): A two-dimensional table of numbers, called a matrix. Vector Creation by Explicit List • A vector in Matlab can be created by an explicit list, starting with a left bracket, entering the values separated by spaces (or commas) and closing the vector with a right bracket. >>x=[0 .1*pi .2*pi .3*pi .4*pi .5*pi .6*pi .7*pi .8*pi .9*pi pi] >>y=sin(x) >>y = Columns 1 through 7 0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 Columns 8 through 11 0.8090 0.5878 0.3090 0.0000 Vector Addressing / indexation • A vector element is addressed in Matlab with an integer index (also called a subscript) enclosed in parentheses. >> x(3) ans = 0.6283 >> y(5) ans = 0.9511 Colon notation: Addresses a block of elements. The format is: (start:increment:end) Note start, increment and end must be positive integer numbers. If the increment is to be 1, a shortened form of the notation may be used: (start:end) >> x(1:5) ans = 0 0.3142 0.6283 0.9425 1.2566 >> x(7:end) ans = 1.8850 2.1991 2.5133 2.8274 3.1416 >> y(3:-1:1) ans = 0.5878 0.3090 >> y([8 2 9 1]) ans = 0.8090 0.3090 0.5878 0 Vector Creation Alternatives • Combining: A vector can also be defined using another vector that has already been defined. >> B = [1.5, 3.1]; >> S = [3.0 B] S = 3.0000 1.5000 3.1000 • Changing: Values can be changed by referencing a specific address >> S(2) = -1.0; >> S S = 3.0000 -1.0000 3.1000 • Extending: Additional values can be added using a reference to a specific address. >> S(4) = 5.5; >> S S = 3.0000 -1.0000 3.1000 5.5000 >> S(7) = 8.5; >> S S = 3.0000 -1.0000 3.1000 5.5000 0 0 8.5000 Vector Creation Alternatives • Colon notation: (start:increment:end) where start, increment, and end can now be floating point numbers. x=(0:0.1:1)*pi x = Columns 1 through 7 0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 Columns 8 through 11 2.1991 2.5133 2.8274 3.1416 • linspace: generates a vector of uniformly incremented values, but instead of specifying the increment, the number of values desired is specified. The form: linspace(start,end,number) The increment is computed internally, having the value: end start increment number 1 Vector Creation Alternatives >> x=linspace(0,pi,11) x = Columns 1 through 7 0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 Columns 8 through 11 2.1991 2.5133 2.8274 3.1416 logspace(start_exponent,end_exponent,number) To create a vector starting at 100 = 1,ending at 102 = 100 and having 11 values: >> logspace(0,2,11) ans = Columns 1 through 7 1.0000 1.5849 2.5119 3.9811 6.3096 10.0000 15.8489 Columns 8 through 11 25.1189 39.8107 63.0957 100.0000 Vector Length length(x): To determine the length of a vector array. >> x = [0 1 2 3 4 5] x = 0 1 2 3 4 5 >> length(x) ans = 6 Vector Orientation A column vector, having one column and multiple rows, can be created by specifying it element by element, separating element values with semicolons: The transpose operator (’) is used to transpose a row vector into a column vector >> c = [1;2;3;4;5] c = 1 2 3 4 5 >> a = 1:5 a = 1 2 3 4 5 >> c = a’ c = 1 2 3 4 5 Matrix arrays in Matlab Vector & Matrix •Use square brackets. •Separate elements on the same row with spaces or commas. •Use semi-colon to go to the next row. A 1 2 3 A= [ 1 2 3 ]; A= [ 1, 2, 3 ]; 1 2 B 3 4 B= [ 1 2 ; 3 4 ]; 5 C 6 7 C= [ 5 ; 6 ; 7 ]; Matrix Arrays A matrix array is 2D, having both multiple rows and multiple columns. Creation of 2D arrays follows that of row and column vectors: – Begin with [ end with ] – Spaces or commas are used to separate elements in a row. – A semicolon or Enter is used to separate rows. >>f = [1 2 3; 4 5 6] f = 1 2 3 4 5 6 >> g = f’ g = 1 4 2 5 3 6 >> h = [1 2 3 4 5 6 7 8 9] h = 1 2 3 4 5 6 7 8 9 >> k = [1 2;3 4 5] ??? Number of elements in each row must be the same. Special matrix creation Manipulations and Combinations: A= Matrix full of 10: >> A=10*ones(2,2) Matrix of random numbers between 0 and 10 >> B=10*rand(2,2) Matrix of random numbers between -1 and 0 >> C= -rand(2,2) Matrix of random numbers between -1 and 1 >> C=2*rand(2,2) –ones(2,2) B= 10 10 10 10 4.5647 8.2141 0.1850 4.4470 C = -0.4103 -0.0579 -0.8936 -0.3529 >> C=2*rand(2,2) -1 C = -0.6475 0.8709 -0.1886 0.8338 Special matrix creation Concatenation: Combine two (or more) matrices into one Notation: Square brackets C=[ A, B ] >> A=ones(2,2); >> B=zeros(2,2); >>C=[A , B] >>D=[A ; B] D= C= 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 Matrix indexation Obtain a single value from a matrix: Notation: 1 2 3 A 3 2 1 1 2 4 Ex: A(2,1) Row index want to know a21 >> A=[1 2 3; 3 2 1; 1 2 4]; >> A(2,1) >> A(3,2) ans = ans = 3 2 Column index Matrix indexation Obtain more than one value from a matrix: Notation: Colon defines a “range”: 1 to 10 Ex: X=1:10 Colon >> A=[1 2 3; 3 2 1; 1 2 4]; Row 1 to 3 A(1:3,2:3) 1 2 3 A 3 2 1 1 2 4 Column 2 to 3 Colon can also be used as a “wildcard” >> B=A(1:3,2:3) B= 2 3 2 1 2 4 Row 2, ALL columns >> C=A(2,:) C= 3 2 1 Matrix size Command Description s = size(A) For an m x n matrix A, returns the two-element row vector s = [m, n] containing the number of rows and columns in the matrix. [r,c] = size(A) [r,c] = size(A) Returns two scalars r and c containing the number of rows and columns in A, respectively. r = size(A,1) Returns the number of rows in A in the variable r. c = size(A,2) Returns the number of columns in A in the variable c. Matrix size >> A = [1 2 3; 4 5 6] A= 123 456 >> s = size(A) s= 23 >> [r,c] = size(A) r= 2 c= 3 >> whos Name Size Bytes Class A 2x3 48 double ans 1x1 8 double c 1x1 8 double r 1x1 8 double s 1x2 16 double array array array array array Special matrix creation zeros(M,N) >> A=zeros(2,3) A= Matrix of zeros ones(M,N) >> B=ones(2,2) B= Matrix of ones eye(M,N) >> C=eye(2,2) C= Matrix of ones on the diagonal rand(M,N) Matrix of random numbers between 0 and 1 >> D=rand(3,2) D= 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0.9501 0.4860 0.2311 0.8913 0.6068 0.7621 Operations on vectors and matrices in Matlab Math Matlab Addition/subtraction A+B A-B Multiplication/ division (element by element) A.*B A./B Multiplication (Matrix Algebra) A*B Transpose: AT A’ Inverse: A-1 inv(A) Determinant: |A| det(A) “single quote” Array Operations Scalar-Array Mathematics Addition, subtraction, multiplication, and division of an array by a scalar simply apply the operation to all elements of the array. >> f = [1 2 3; 4 5 6] f = 1 2 3 4 5 6 >> g = 2*f -1 g = 1 3 5 7 9 11 Array Operations Element-by-Element Array-Array Mathematics When two arrays have the same dimensions, addition, subtraction, multiplication, and division apply on an element-by-element basis. Operation Addition Subtraction Multiplication Division Exponentiation Algebraic Form a+b a−b axb a/b ab Matlab a + b a - b a.*b a./b a.^b Array Operations MATRIX Addition (substraction) a11 a12 A a 21 a 22 a 31 a 32 a13 a 23 a 33 M b11 b12 B b 21 b 22 b 31 b 32 b13 b 23 b33 N N a11 b11 a12 b12 A B a21 b21 a22 b22 a b 31 31 a32 b32 N a13 b13 a23 b23 a33 b33 M M Array Operations Examples: Addition & Subtraction 1 2 3 B 4 5 6 7 8 9 1 2 3 A 4 5 6 7 8 9 AB 2 4 6 8 10 12 14 16 18 AB 0 0 0 0 0 0 0 0 0 Array Operations Element-by-Element Array-Array Mathematics >> A >> B >> C C = 4 15 = [2 5 6]; = [2 3 5]; = A.*B 30 >> D = A./B D = 1.0000 1.6667 1.2000 >> E = A.^B E = 4 125 7776 >> F = 3.0.^A F = 9 243 729 Array Operations MATRIX Multiplication (element by element) a11 a12 A a 21 a 22 a 31 a 32 a13 a 23 a 33 M b11 b12 B b 21 b 22 b 31 b 32 b13 b 23 b33 N N NOTATION “dot” “multiply” a11 b11 a12 b12 A B a21 b21 a22 b22 a b 31 31 a32 b32 N a13 b13 a23 b23 M a33 b33 M Array Operations Examples: Multiplication & Division (element by element) 1 2 3 B 4 5 6 7 8 9 1 2 3 A 4 5 6 7 8 9 A B 1 4 9 16 25 36 49 64 81 A / B 1 1 1 1 1 1 1 1 1 Array Operations Matrix Multiplication The matrix multiplication of m x n matrix A and nxp matrix B yields m x p matrix C, denoted by C = AB Element cij is the inner product of row i of A and column j of B n cij aik bkj k 1 Note that AB ≠ BA Array Operations a11 a12 A a 21 a 22 a 31 a 32 a13 Row 1 a 23 M1 a 33 Column 1 Matrix Multiplication b11 b12 B b 21 b 22 b 31 b 32 N1 b13 b 23 M2 b33 N2 N1=M2 NOTATION a11b11 a12b 21 a13b31 a11b12 a12b 22 a13b32 A B a 21b11 a 22b 21 a 23b31 a 21b12 a 22b 22 a 21b32 a b a b a b 31 11 32 21 33 31 a 31b12 a 32b 22 a 33b32 N2 “multiply” a11b13 a12b 23 a13b33 a 21b13 a 22b 23 a 23b33 M1 a 31b13 a 32b 23 a 33b33 Array Operations Example: Matrix Multiplication 1 2 3 A 3 2 1 3 1 2 AB 1 2 3 B 3 2 1 3 1 2 1x1 + 2x3 +3x3 1x2 + 2x2 +3x1 1x3 + 2x1 +3x2 16 9 11 12 11 13 12 10 14 Array Operations Solving systems of linear equations Example: 3 equations and 3 unknown 1x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 Can be easily solved by hand, but what can we do if it we have 10 or 100 equations? Array Operations Solving systems of linear equations 1x + 6y + 7z = 0 2x + 5y + 8z = 1 3x + 4y + 5z = 2 First, write a matrix with all the (xyz) coefficients Write a matrix with all the constants 0 B 1 2 1 6 7 A 2 5 8 3 4 5 Finally, consider the matrix of unknowns x S y z Array Operations Solving systems of linear equations AxS=B A-1 x A x S = A-1 x B (A-1 x A) x S = A-1 x B I x S = A-1 x B S = A-1 x B Array Operations Solving systems of linear equations The previous set of equations can be expressed in the following vector-matrix form: AxS=B 1x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 1 6 7 x 0 2 5 8 X y 1 3 4 5 z 2 Array Operations Matrix Determinant •The determinant of a square matrix is a very useful value for finding if a system of equations has a solution or not. •If it is equal to zero, there is no solution. Notation: Determinant of A = |A| or det(A) Formula for a 2x2 matrix: m11 m12 M m 21 m 22 det(M)= m11 m22 – m21 m12 IMPORTANT: the determinant of a matrix is a scalar Array Operations Matrix Inverse •The inverse of a matrix is really important concept, for matrix algebra •Calculating a matrix inverse is very tedious for matrices bigger than 2x2. We will do that numerically with Matlab. Notation: inverse of A = A-1 or inv(A) Formula for a 2x2 matrix: m11 m12 M m 21 m 22 M-1= 1 m22 m12 det( M ) m21 m11 IMPORTANT: the inverse of a matrix is a matrix Array Operations Matrices properties Property of inverse : A x A-1 = I and A-1 x A = I Example: 0.6 1 0 0 1 1 2 0.4 0.2 1 2 1 x 0.2 0.6 0.2 0 1 0 2 0 1 0.8 0.4 0.2 0 0 1 Property of identity matrix: and IxA=A AxI=A Solving systems of equations in Matlab x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 5z =2 In Matlab: >> A=[ 1 6 7; 2 5 8; 3 4 5] >> B=[0;1;2]; >> S=inv(A)*B 1 6 7 x A 2 5 8 S y 3 4 5 z Verification: >> det(A) ans = >> S= 0.8571 -0.1429 0 28 0 B 1 2 Solving systems of equations in Matlab x + 6y + 7z =0 2x + 5y + 8z =1 3x + 4y + 9z =2 In Matlab: >> A=[ 1 6 7; 2 5 8; 3 4 5] >> B=[0;1;2]; >> S=inv(A)*B 1 6 7 x A 2 5 8 S y 3 4 9 z 0 B 1 2 Verification: >> det(A) ans = Warning: Matrix is singular to working precision. >> S= NaN NaN NaN 0 NO Solution!!!!! Applications in mechanical engineering y F1 7N 20o 30o 5N 60o 80o F2 Find the value of the forces F1and F2 x Applications in mechanical engineering y F1 7N 20o 30o 5N Projections on the X axis 60o x 80o F2 F1 cos(60) + F2 cos(80) – 7 cos(20) – 5 cos(30) = 0 Applications in mechanical engineering y F1 7N 20o 30o 5N Projections on the Y axis 60o x 80o F2 F1 sin(60) - F2 sin(80) + 7 sin(20) – 5 sin(30) = 0 Applications in mechanical engineering F1 cos(60) + F2 cos(80) – 7 cos(20) – 5 cos(30) = 0 F1 sin(60) - F2 sin(80) + 7 sin(20) – 5 sin(30) =0 F1 cos(60) + F2 cos(80) F1 sin(60) - F2 sin(80) = 7 cos(20) + 5 cos(30) = - 7 sin(20) + 5 sin(30) In Matlab, sin and cos use radians, not degree In Matlab: >> CF=pi/180; >> A=[cos(60*CF), cos(80*CF) ; sin(60*CF), –sin(80*CF)]; >> B=[7*cos(20*CF)+5*cos(30*CF) ; -7*sin(20*CF)+5*sin(30*CF) ] >> F= inv(A)*B or (A\B) Solution: F= F1= 16.7406 N 16.7406 F2= 14.6139 N 14.6139