200 likes | 271 Views
Week 3. Last week Vectors Array Addressing Mathematical Operations Array Multiplication and Division Identity Matrix Inverse of a Matrix Element by Element Calculations. Array Addressing. A = 1 2 3 4 5 6 7 8 9 >> B = A([2 3],[2 3]) B =
E N D
Week 3 • Last week • Vectors • Array Addressing • Mathematical Operations • Array Multiplication and Division • Identity Matrix • Inverse of a Matrix • Element by Element Calculations
Array Addressing A = 1 2 3 4 5 6 7 8 9 >> B = A([2 3],[2 3]) B = 5 6 8 9 >> B = A(2:3,2:3) B = 5 6 8 9
Vectors >> q = 0:0.1:2*pi; % semicolon suppresses output >> y=sin(q); % Comment after % is ignored >> plot(q,y)
TransposeInterchange rows and columns >> X = [1 2 3 4 5 6 7 8 9 10] X = 1 2 3 4 5 6 7 8 9 10 >> X' ans = 1 6 2 7 3 8 4 9 5 10
Vector Functions A = [ 1 2 3 4 5 6] sum(A) = 21 max(A) = 6 min(A) = 1
Random Arrays >> A = rand(4,3) A = 0.9501 0.8913 0.8214 0.2311 0.7621 0.4447 0.6068 0.4565 0.6154 0.4860 0.0185 0.7919
Sound Files sound_in.m creates an array called data. data is an array of one column with 10,000 rows Sounds can be saved under different names Yes = data; and = data; No = data; Arrays can be concatenated to form longer sounds yesandNo = [Yes; and; No]
Array Multiplication • Y = A*B • The number of columns in A must equal the number of rows in B.
Multiplication Example • C(1,1) = 1*1 + 2*2 + 3*3 = 14 • C(1,2) = 1*2 + 2*1 + 3*1 = 7 • C(2,1) = 3*1 + 2*2 + 1*3 = 10 • C(2,2) = 3*2 + 2*1 + 1*1 = 9 • C(3,1) = • C(3,2) = • C(4,1) = • C(4,2) =
Errors A = 1 2 3 4 5 6 7 8 9 >> C= ones(3,2) C = 1 1 1 1 1 1 >> A*C ans = 6 6 15 15 24 24 >> C*A ??? Error using ==> * Inner matrix dimensions must agree.
Example >> BV = [3; 1; 4] BV = 3 1 4 >> AV = [2 5 1] AV = 2 5 1 % AV*BV is the dot product >> AV*BV % The number of columns in ans = % AV equals the number of 15 % rows in BV % AV*BV is a scalar >> BV*AV % Rows in BV = 3 ans = % Columns in AV = 3 6 15 3 % BV&AV is a 3x3 array 2 5 1 8 20 4
Identity Matrix >> I = eye(3) I = 1 0 0 0 1 0 0 0 1
Identity Matrix • Multiplying a matrix by the identity matrix is equivalent to multiplication by one >> I = eye(3) I = 1 0 0 0 1 0 0 0 1 If A is square AI = IA = A
The matrix B is the inverse of the matrix A if BA = AB = I where I is the identity matrix >> A =[ 1 2; 2 3] A = 1 2 2 3 >> B = A^-1 B = -3 2 2 -1 >> A*B ans = 1 0 0 1 Inverse of a Matrix
Array Division Solve for X where A*X = B A and B are known arrays. A^-1 *A*X = A^-1 *B Since A^-1* A = I X = A^-1* B Left Division X = A\B
2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = 2 -3 4 1 6 -3 -5 1 2 B = 10 4 3 >> X = A\B X = 1.2609 2.2261 3.5391 >> 2*X(1) -3*X(2) + 4*X(3) ans = 10 Division Example
Element by Element Operations • If the usual symbols (* / ^) are used operations follow the rules of linear algebra. Sometimes we don’t want this. • Given x = [1 2 3] • And y = [2 4 6] • Find z = [x(1)*y(1), x(2)*y(2), x(3)*y(3)] • Use the dot product operator .* • z = x .*y = [2 8 18]
Element by Element Operators • Multiplication .* x(n) = A(n)*B(n) • Division ./ x(n) = A(n)/B(n) • Exponentiation .^ x(n) = A(n)^y(n) • Left Division .\ x(n) = B(n)/A(n) • x = A .*B • x = A ./ B • x = A .^ y • x = A .\B
Element by Element Example A = [ 0 1 2 3 4] B = [ 5 4 3 2 1] >> A .*B ans = 0 4 6 6 4 >> A ./B ans = 0 0.2500 0.6667 1.5000 4.0000 >> A .^2 ans = 0 1 4 9 16
Element by Element Calculation • >> x= 0:0.1:10; • >> y = x .^2; • >> plot(x,y)