Download presentation
Presentation is loading. Please wait.
Published byΠροκόπιος Γιαννόπουλος Modified over 5 years ago
1
‘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
2
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
3
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)
4
You can apply relational and logical operators to entire arrays in Matlab
Given that x = [ ] and y = [ ], 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
5
Selecting a subset of an array or matrix
x=[ ; ; ; ]; Try: x>0 x(x>0) Also try: [ii,jj]=find(x>0) x(find(x>0))
6
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?
7
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.
8
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
9
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.
10
Write a script named checkerboad which uses a double for loop and if statement(s) to generate the matrix check = You can then visualize this matrix in a cool way by typing imagesc(check), colormap(gray)
11
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.