350 likes | 494 Vues
Computer Simulation Lab. “Lecture 4”. Electrical and Computer Engineering Department SUNY – New Paltz. Logical vectors. objectives L ogical operators Logical vectors Logical functions. Logical Operators. r = 0; r <= 0.5 ans= 1. r = .5; r <= 0.5 ans= 1. r = 1; r <= 0.5 ans= 0.
E N D
Computer Simulation Lab “Lecture 4” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Logical vectors objectives • Logical operators • Logical vectors • Logical functions SUNY-New Paltz
Logical Operators r = 0; r <= 0.5 ans= 1 r = .5; r <= 0.5 ans= 1 r = 1; r <= 0.5 ans= 0 r = 1:5; r <= 3 ans= 1 1 1 0 0 a = 1:5; b = [0 2 3 5 6]; a == b ans= 0 1 1 0 0 SUNY-New Paltz
Application of Logical Vectors 1-form vector 0<x<3*pi 2- form a vector such that if y > 0 then it is one Multiply y by this vector x = 0 : pi/20 : 3 * pi; y = sin(x); y = y .* (y > 0); plot(x, y) SUNY-New Paltz
Avoiding Division by Zero 1-form vector x -4*pi <x< 4*pi 2- form a new vector x such that if x=0, then it is eps 3- Divide sin(x) by this vector x = -4*pi : pi/20 : 4*pi; x = x + (x == 0)*eps; y = sin(x) ./ x; plot(x, y) Sin(ε)/ε=1 SUNY-New Paltz
Logical Operators SUNY-New Paltz
Operator Precedence SUNY-New Paltz
Be Careful! 0 < r < 1Wrong! (0 < r) & (r < 1) Correct! SUNY-New Paltz
Subscripting with logical vectors a = [-2 0 1 5 9]; a([5 1 3]) ans: 9 -2 1 a(logical([0 1 0 1 0])) ans: 0 5 a = a(a > 0) ans: 1 5 9 SUNY-New Paltz
Verifying Logical Vectors a = [ 1 –1 2 0 3] b= a > 0 islogical(a) ans: 0 islogical(b) ans: 1 SUNY-New Paltz
Logical functions any(x) all(x) exist(’a’) find(x) returns the scalar 1 (true) if any element of x is non-zero (true). returns the scalar 1 if all the elements of x are non-zero. returns 1 if a is a workspace variable. returns a vector containing the subscripts of the non-zero (true) elements of x SUNY-New Paltz
Examples a=[0 2 1 2 3 4] a = a( find(a) ) x = [8 1 -4 8 6]; find(x >= max(x)) SUNY-New Paltz
Logical functions isempty(x) isinf(x) isnan(x) returns 1 if x is an empty array and 0 otherwise. returns 1’s for the elements of x which are +Inf or −Inf, and 0’s otherwise returns 1’s where the elements of x are NaN and 0’s otherwise. Example: x(isnan(x)) = [ ] SUNY-New Paltz
Using ‘any’ and ‘all’ if any(a ~= b) statement end if a ~= b statement end if all(a >= 1) do something end SUNY-New Paltz
Logical vectors instead of ‘elseif’ ladders SUNY-New Paltz
Matlab Code Using if/elseif inc =[5000 10000 15000 30000 50000]; for ti = inc if ti < 10000 tax = 0.1 * ti; elseif ti < 20000 tax = 1000 + 0.2 * (ti - 10000); else tax = 3000 + 0.5 * (ti - 20000); end; disp( [ti tax] ) end; SUNY-New Paltz
Logical Way inc = [5000 10000 15000 30000 50000]; tax = 0.1 * inc .* (inc <= 10000); tax = tax+(inc > 10000 & inc <= 20000) ... .* (0.2 * (inc-10000) + 1000); tax = tax + (inc > 20000) .* (0.5*(inc-20000)+ 3000); disp( [inc’ tax’] ); SUNY-New Paltz
Matrices Objectives •ways of creating and manipulating matrices; •matrix operations. SUNY-New Paltz
Creating matrices a = [1 2; 3 4]; x = [5 6]; a = [a; x] b = a’ b= 1 3 5 2 4 6 a = 1 2 3 4 5 6 SUNY-New Paltz
Subscripts a = 1 2 3 4 5 6 7 8 9 x = [0:30:180]’; trig(:,1) = x; trig(:,2) = sin(pi/180*x); trig(:,3) = cos(pi/180*x); a(3,3) a(2:3,1:2) a(3,:) a(1:2,2:3) = ones(2) trig = 0 0 1.0000 30.0000 0.5000 0.8660 60.0000 0.8660 0.5000 90.0000 1.0000 0.0000 120.0000 0.8660 -0.5000 150.0000 0.5000 -0.8660 180.0000 0.0000 -1.0000 SUNY-New Paltz
‘:’ Operator b = 1 2 3 4 5 6 a = 0 0 0 0 0 0 a(:) = b a = 1 2 3 4 b = a(:) b= 1 2 3 4 a = 1 5 4 3 2 6 SUNY-New Paltz
Duplicating rows and columns: tiling a = 1 2 3 repmat(a, [3 1]) or repmat(a, 3, 1) ans = 1 2 3 1 2 3 1 2 3 SUNY-New Paltz
Exercise 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 3 5 7 9 11 13 15 17 19 10 9 8 7 6 5 4 3 2 1 1 3 5 7 9 11 13 15 17 19 10 9 8 7 6 5 4 3 2 1 1 3 5 7 9 11 13 15 17 19 10 9 8 7 6 5 4 3 2 1 1 3 5 7 9 11 13 15 17 19 10 9 8 7 6 5 4 3 2 1 4 3 4 3 4 3 1 2 1 2 1 2 4 3 4 3 4 3 1 2 1 2 1 2 SUNY-New Paltz
Deleting rows and columns a = 1 2 3 4 5 6 7 8 9 a(:,2) = [ ] a(1,2) = [ ] a(2:2:6) = [ ] a(:, logical([1 0 1])) ans = 1 3 4 6 7 9 a = 1 7 5 3 6 9 SUNY-New Paltz
Elementary matrices eye(3) ans = 1 0 0 0 1 0 0 0 1 pascal(4) ans = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 SUNY-New Paltz
Generating Specialized Matrices compan - Companion matrix. gallery - Higham test matrices. hadamard - Hadamard matrix. hankel - Hankel matrix. hilb - Hilbert matrix. invhilb - Inverse Hilbert matrix. magic - Magic square. pascal - Pascal matrix. rosser - Classic symmetric eigenvalue toeplitz - Toeplitz matrix. vander - Vandermonde matrix. wilkinson - Wilkinson's eigenvalue test matrix. SUNY-New Paltz
Using MATLAB functions with matrices a = 1 0 1 1 1 1 0 0 1 all(a) any(a) 0 0 1 1 1 1 SUNY-New Paltz
Manipulating matrices diag fliplr flipud rot90 tril extracts or creates a diagonal. flips from left to right. flips from top to bottom. Rotate 90 egrees Extract Lower Triangle SUNY-New Paltz
Array (element-by-element) operations on matrices a = 1 2 3 4 5 6 a .^ 2 ans = 1 4 9 16 25 36 a = 1 2 3 4 5 6 7 8 9 for v = a disp(v’) end 1 4 7 2 5 8 3 6 9 SUNY-New Paltz
Visualization of matrices A = 1000; % amount borrowed n = 12; % number of payments per year for r = 0.1 : 0.01 : 0.2 fprintf( ’%4.0f%’, 100 * r ); for k = 15 : 5 : 25 temp = (1 + r/n) ^ (n*k); P = r * A * temp / n / (temp - 1); fprintf( ’%10.2f’, P ); end; fprintf( ’\n’ ); % new line end; SUNY-New Paltz
Vectorize the Problem for r = 0.1 : 0.01 : 0.2 k = 15 : 5 : 25; temp = (1 + r/n) .^ (n*k); P = r * A * temp / n ./ (temp - 1); disp( [100 * r, P] ); end; SUNY-New Paltz
Eliminate for loops! r = [0.1:0.01:0.2]’ r = repmat(r, [1 3]) k = 15:5:25 k = repmat(k, [11 1]) temp = (1 + r/n) .^ (n * k); P = r * A .* temp / n ./ (temp - 1) SUNY-New Paltz
Multi-dimensional arrays a = [1:2; 3:4] a(:,:,2) = [5:6; 7:8] a(:,:,1) = 1 2 3 4 a(:,:,2) = 5 6 7 8 SUNY-New Paltz
Matrix operations Matrix multiplication SUNY-New Paltz
Other matrix functions det determinant. eigeigenvalue decomposition. expmmatrix exponential, i.e. eA, where A is a matrix. invinverse. luLU factorization (into lower and upper triangular matrices). qrorthogonal factorization. svdsingular value decomposition SUNY-New Paltz