Presentation is loading. Please wait.

Presentation is loading. Please wait.

Electrical and Computer Engineering Department SUNY – New Paltz

Similar presentations


Presentation on theme: "Electrical and Computer Engineering Department SUNY – New Paltz"— Presentation transcript:

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

2 Logical vectors objectives Logical operators Logical vectors
Logical functions SUNY-New Paltz

3 Logical Operators r = 1; r <= 0.5 ans= 0 r = 0; r <= 0.5 ans= 1
b = [ ]; a == b ans= SUNY-New Paltz

4 Exercise Prompt the user to enter a vector of numbers between 1 and 10. Your program should print the total number of elements that are less than 5. Hint: No for loop are required. SUNY-New Paltz

5 Logical Operators SUNY-New Paltz

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

7 Operator Precedence SUNY-New Paltz

8 Exercise Prompt the user to enter a vector of numbers between 1 and 10. Your program should print the total number of elements that lie between 1 and 5. Hint: No for loop are required. Repeat the above program but this time print out the number of elements between 3 and 7. SUNY-New Paltz

9 Subscripting with logical vectors
ans: a(logical([ ])) ans: 0 5 a = a(a > 0) ans: 1 5 9 SUNY-New Paltz

10 Exercise Prompt the user to enter a vector of numbers between -10 and 10. Your program should separate the input vector into 2 vectors: 1- a vector containing the positive and zero numbers. 2- anther vector containing the negative numbers. Hint: No for loop are required. SUNY-New Paltz

11 Exercise Prompt the user to enter a vector (no zeros in the vector). Then change the values of the elements with odd indices to 0. Hint: use an array of indices Prompt the user to enter a vector (no zeros in the vector). Then change the values of the elements with negative values to 0. Hint: use a logical vector of zeros and ones SUNY-New Paltz

12 Verifying Logical Vectors
b= a > 0 islogical(a) ans: 0 islogical(b) ans: 1 SUNY-New Paltz

13 Subscripting with logical vectors Exercise
Prompt the user to enter a vector of a set of numbers between 0 and 9 of any length. Your program should use a for loop to iterate through the vector and print the total number of elements that lie between 4 and 7 (inclusive). SUNY-New Paltz

14 Subscripting with logical vectors Exercise
Repeat the same program using logical vectors. Hint: generate a logical vector out of the user input vector and use the sum() function to get the total. SUNY-New Paltz

15 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

16 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

17 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

18 Logical functions Exercises
Prompt the user to enter a vector of numbers between 0 and 9. Your program should analyze the input vector and respond by printing either: The vector has no zeros. The vector has XX non-Zero elements. The first non-zero is at location YY SUNY-New Paltz

19 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

20 Using ‘any’ and ‘all’ if any(a ~= b) statement end if all(a >= 1)
do something end SUNY-New Paltz

21 Exercise Write a program that initially generates 2 random vectors of size 3 (having numbers 1 through 3) – use the randi() function. Then compare the vectors and respond as: The vectors are equal or The vectors have x elements in common or The vectors have nothing in common SUNY-New Paltz

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

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

24 Subscripts x = [0:30:180]’; trig(:,1) = x; trig(:,2) = sin(pi/180*x);
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 = SUNY-New Paltz

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

26 Duplicating rows and columns: tiling
repmat(a, [3 1]) or repmat(a, 3, 1) ans = SUNY-New Paltz

27 Exercise SUNY-New Paltz

28 Exercise Generate a vector that represents a square waive and plot it.
Hits: use ones() and zeros() along with repmat(). SUNY-New Paltz

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

30 Elementary matrices eye(3) ans = 1 0 0 0 1 0 0 0 1 pascal(4) ans =
SUNY-New Paltz

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

32 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

33 Array (element-by-element) operations on matrices
1 2 3 4 5 6 a .^ 2 ans = 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

34 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

35 Multi-dimensional arrays
1 2 3 4 a(:,:,2) = 5 6 7 8 SUNY-New Paltz

36 Matrix operations Matrix multiplication SUNY-New Paltz

37 Solution to Exercises clc clear all if(any(v) == 0) disp('No Zeros!');
else w = find(v); disp(['has ''' num2str(length(w)) ''' zeros']); end disp(['first zero index = ' num2str(w(1))]) SUNY-New Paltz

38 Solution to Exercises clc clear all % for loop approach sum=0;
v = input('Enter a vector of integers betwwen 0 and 9: '); for i=v if(i >= 4 & i <= 7) sum=sum+1; end disp(['total = ' num2str(sum)]) % logical vector approach disp(sum(v >= 4 & v<=7)) SUNY-New Paltz


Download ppt "Electrical and Computer Engineering Department SUNY – New Paltz"

Similar presentations


Ads by Google