Presentation is loading. Please wait.

Presentation is loading. Please wait.

SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”

Similar presentations


Presentation on theme: "SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”"— Presentation transcript:

1 SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”

2 SUNY-New Paltz Logical vectors objectives 1.Logical operators 2.Logical vectors 3.Logical functions

3 SUNY-New Paltz Logical Operators r = 1; r <= 0.5 ans= 0 r = 0; r <= 0.5 ans=1 r =.5; r <= 0.5 ans=1 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

4 SUNY-New Paltz Application of Logical Vectors x = 0 : pi/20 : 3 * pi; y = sin(x); y = y.* (y > 0); plot(x, y) 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

5 SUNY-New Paltz Avoiding Division by Zero x = -4*pi : pi/20 : 4*pi; x = x + (x == 0)*eps; y = sin(x)./ x; plot(x, y) Sin(ε)/ε=1 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

6 SUNY-New Paltz Logical Operators

7 SUNY-New Paltz Operator Precedence

8 SUNY-New Paltz Be Careful! 0 < r < 1 Wrong! (0 < r) & (r < 1) Correct!

9 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

10 SUNY-New Paltz Verifying Logical Vectors a = [ 1 –1 2 0 3] b= a > 0 islogical(a) ans: 0 islogical(b) ans: 1

11 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

12 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))

13 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)) = [ ]

14 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

15 SUNY-New Paltz Logical vectors instead of ‘elseif’ ladders

16 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;

17 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’] );

18 SUNY-New Paltz Matrices Objectives ways of creating and manipulating matrices; matrix operations.

19 SUNY-New Paltz Creating matrices a = [1 2; 3 4]; x = [5 6]; a = [a; x] a = 1 2 3 4 5 6 b = a’ b= 1 3 5 2 4 6

20 SUNY-New Paltz Subscripts a(3,3) a(2:3,1:2) a(3,:) a(1:2,2:3) = ones(2) x = [0:30:180]’; trig(:,1) = x; trig(:,2) = sin(pi/180*x); trig(:,3) = cos(pi/180*x); a = 1 2 3 4 5 6 7 8 9 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

21 SUNY-New Paltz ‘:’ Operator a = 1 2 3 4 b = a(:) b= 1 2 3 4 b = 1 2 3 4 5 6 a = 0 a(:) = b a = 1 5 4 3 2 6

22 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

23 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 4 3 4 3 4 3 1 2 1 2 1 2 4 3 4 3 4 3 1 2 1 2 1 2 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

24 SUNY-New Paltz Deleting rows and columns a(:,2) = [ ] a(1,2) = [ ] a(2:2:6) = [ ] a(:, logical([1 0 1])) ans = 1 3 4 6 7 9 a = 1 2 3 4 5 6 7 8 9 a = 1 7 5 3 6 9

25 SUNY-New Paltz Elementary matrices eye(3) ans = 1 0 0 0 1 0 0 0 1 pascal(4) ans = 1 1 1 2 3 4 1 3 6 10 1 4 10 20

26 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.

27 SUNY-New Paltz Using MATLAB functions with matrices a = 1 0 1 1 1 1 0 0 1 all(a) any(a) 1 1 1 0 0 1

28 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

29 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

30 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;

31 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;

32 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)

33 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

34 SUNY-New Paltz Matrix operations Matrix multiplication

35 SUNY-New Paltz Other matrix functions det determinant. eig eigenvalue decomposition. expm matrix exponential, i.e. eA, where A is a matrix. inv inverse. lu LU factorization (into lower and upper triangular matrices). qr orthogonal factorization. svd singular value decomposition


Download ppt "SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”"

Similar presentations


Ads by Google