Electrical and Computer Engineering Department SUNY – New Paltz

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Introduction to Matlab
Lecture 4.
Maths for Computer Graphics
Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Chapter 10 Review: Matrix Algebra
1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 4”
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Working with Arrays in MATLAB
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
The MatLab Language Mathematics Laboratory The MathWorks
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Matrix Mayhem: Outline
Manipulating MATLAB Matrices Chapter 4
Chapter 7 Matrix Mathematics
1.5 Matricies.
Repetition Structures Chapter 9
EGR 115 Introduction to Computing for Engineers
CS1371 Introduction to Computing for Engineers
Computer Simulation Lab
Chapter 8: Lesson 8.1 Matrices & Systems of Equations
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATLAB PRIMER by Michael Medvinsky(2014)
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
Yang-Ming University, Taipei, Taiwan
Vectorized Code, Logical Indexing
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Digital Image Processing
CS 175 Project in AI Discussion -- matlab
Loop Statements & Vectorizing Code
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lets Play with arrays Singh Tripty
Announcements P3 due today
Matlab Training Session 2: Matrix Operations and Relational Operators
Matlab Basics.
Loop Statements & Vectorizing Code
EECS Introduction to Computing for the Physical Sciences
DETERMINANT MATH 80 - Linear Algebra.
Matrices are identified by their size.
Working with Arrays in MATLAB
General Computer Science for Engineers CISC 106 Lecture 15
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Computer Simulation Lab
Computer Simulation Lab
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Electrical and Computer Engineering Department SUNY – New Paltz Computer Simulation “Lecture 6” 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 = 1; r <= 0.5 ans= 0 r = 0; r <= 0.5 ans= 1 b = [0 2 3 5 6]; a == b ans= 0 1 1 0 0 SUNY-New Paltz

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

Logical Operators SUNY-New Paltz

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

Operator Precedence SUNY-New Paltz

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

Subscripting with logical vectors 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

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

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

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

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

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

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

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

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 all(a >= 1) do something end SUNY-New Paltz

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

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

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

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 = 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 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

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

Exercise 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 4 3 4 3 4 3 1 2 1 2 1 2 SUNY-New Paltz

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

Deleting rows and columns 1 2 3 4 5 6 7 8 9 a(:, logical([1 0 1])) ans = 1 3 4 6 7 9 a(:,2) = [ ] a(1,2) = [ ] a(2:2:6) = [ ] 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

Using MATLAB functions with matrices 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 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

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 1 2 3 4 a(:,:,2) = 5 6 7 8 SUNY-New Paltz

Matrix operations Matrix multiplication SUNY-New Paltz

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

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