‘If’ statements, relational operators, and logical operators

Slides:



Advertisements
Similar presentations
Bison Management Suppose you take over the management of a certain Bison population. The population dynamics are similar to those of the population we.
Advertisements

Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Final Project Part II MATLAB Session ES 156 Signals and Systems 2007 SEAS Prepared by Frank Tompkins.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
MATLAB TUTORIAL Dmitry Drutskoy Some material borrowed from the departmental MATLAB info session by Philippe Rigollet Kevin Wayne.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Measures of Variability Objective: Students should know what a variance and standard deviation are and for what type of data they typically used.
CIS 2033 based on Dekking et al. A Modern Introduction to Probability and Statistics Michael Baron. Probability and Statistics for Computer Scientists,
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Matlab Basics FIN250f: Lecture 3 Spring 2010 Grifths Web Notes.
PATTERN RECOGNITION LAB 3 TA : Nouf Al-Harbi::
Continuous Random Variables Lecture 24 Section Tue, Mar 7, 2006.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Digital Image Processing. Converting between Data Classes The general syntax is B = data_class_name (A) Where data_class_name is one of the names defined.
Continuous Random Variables Lecture 24 Section Tue, Oct 18, 2005.
PATTERN RECOGNITION LAB 2 TA : Nouf Al-Harbi::
Control Structures Hara URL:
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Naïve Bayes Classification Recitation, 1/25/07 Jonathan Huang.
Stats Lab #3.
List manipulation;curve fitting
Statistical Inference
Modelling and Simulating Social Systems with MATLAB
Linear Algebra Review.
EEE 161 Applied Electromagnetics
Prof. Mark Glauser Created by: David Marr
Artificial Neural Networks
Modelling and Simulating Social Systems with MATLAB
Computer Programming BCT 1113
L – Modeling and Simulating Social Systems with MATLAB
Introduction to MATLAB
EGR 115 Introduction to Computing for Engineers
Chapter 4 MATLAB Programming
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
User Defined Functions
Arrays & Functions Lesson xx
CS005 Introduction to Programming
StatLab Matlab Workshop
Visual Basic .NET BASICS
Writing Methods AP Computer Science A.
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
The Power of Calling a Method from Itself
Digital Image Processing
Formulas for Linear Functions
Logical Operations In Matlab.
Programming We have seen various examples of programming languages
Introduction to Matlab LAB 4
Multidimensional Arrays
BASIC MATLAB PROGRAMMING
Continuous Random Variables
Continuous Random Variables
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.
Introduction to MATLAB
Problem 1.
The lognormal distribution
Matlab Basics.
EE 194/BIO 196: Modeling biological systems
Random numbers What does it mean for a number to be random?
First Semester Review.
Continuous Random Variables
Presentation transcript:

‘If’ statements, relational operators, and logical operators By the end of this lesson, you should be able to identify when logical and relational operators should be used in an algorithm implement logical and relational operators in your code generate random numbers in Matlab

Relational operators x=5; y=8; x<y y<x x==y x~=y x>5 x>=5 Beware the difference between == and = Now set x=5. What happens when you try 6 > x > 2 ? --For last question, say that to do this operation you need to use logical operators

Logical operators So what should we use instead of 6>x>2 ? Symbol Effect & logical AND | logical OR ~ logical NOT logical Converts all non-zero values to 1 any Returns 1 if ANY elements of vector are non-zero all Returns 1 if ALL elements of vector are non-zero x=5 y=8 (x==y) & (x<y) (x==y) | (x<y) (x==y) | ~(x<y) --show how to do 2 < x < 6 So what should we use instead of 6>x>2 ? (6>x) & (x>2)

You can apply relational and logical operators to entire arrays in Matlab Given that x = [1 5 2 8 9 0 1] and y = [5 2 2 6 0 0 2], first predict the result of each line of code, then use Matlab to check your prediction: x > y x <= y b. x == y c. x = y --have them work through this in pairs

Selecting a subset of an array or matrix x=[1 -1 2 4; -3 1 -2 6; 5 7 -5 8; 6 -2 -1 -9]; Try: x>0 x(x>0) Also try: [ii,jj]=find(x>0) x(find(x>0))

Type rand. Then type rand(5,10). Then type randn(5,10) Type rand. Then type rand(5,10). Then type randn(5,10). What is the difference between the rand and randn functions?

Create a vector with 20 elements, all pulled from a normal distribution. Then use the find function to set equal to 0 all the elements that are less than 0. BONUS: Generate a vector with 20 elements, all pulled from a normal distribution. Then write code that finds the sum of just the negative values. You can create the vector and find this sum with just two lines of code.

If statements Suppose x is the amount of money in your checking account. Then you can use an ‘if’ statement to determine whether or not you are overdrawn: if(x>=0) disp(‘You are in the black.’) else disp(‘You are overdrawn.’) end

Neurons are often modeled to fire randomly, but with a particular mean frequency. Start by writing spikes=zeros(1,100). Each entry of this vector will represent one second of recording from the neuron. We will place a 1 in a particular entry if the neuron spikes during that corresponding second of time. (For example, if spikes(24)=1, that means the neuron spiked during the 24th second.) Your job is to write a script spiking_model in which you begin the code by specifying a parameter p, which is the probability of the neuron spiking during any one-second time interval. Then use a for loop to go through each entry of spikes and determine whether or not the neuron spiked in each interval. Then use the command stem(spikes) to visualize the neuron’s spike train.

Write a script named checkerboad which uses a double for loop and if statement(s) to generate the matrix check = 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 You can then visualize this matrix in a cool way by typing imagesc(check), colormap(gray)

Exercise using an if statement Suppose we want an array of random numbers that are drawn from only the positive part of a Gaussian distribution. Write a script where at the top of the script, a variable N stores the total number of Gaussian numbers you want, and then the rest of the code generates these Gaussian random numbers and stores only the positive ones in a vector. Name your script pos_gauss. --created file pos_gauss which does this, if I need a reference