Sometimes when we write scripts we want to have the flexibility to use conditional logic. An example in the real world might be IF it is rainy then I will go to the gym but IF it is sunny then I will go for a run. Here is a bit of code that simulates this dilemma. Note, this code uses a new command, the rand command which generates random numbers. This will cause us to have to see the random number generator. This is a complex idea - more on this can be found HERE but you can simply trust me that we have to do it. Let's assume that there is a 30 percent chance it rains on any given day.
clc;
clear all;
close all;
rng('shuffle');
This last command seeds the random number generator so any generated numbers are truly "random". If you do not use this, you will find that your so called random numbers always follow the same pattern. Now, for the rest of the script.
days_running = 0;
days_gym = 0;
for day_counter = 1:7
did_it_rain = rand(1);
if did_it_rain <= 0.3
days_gym = days_gym + 1;
else
days_running = days_running + 1;
end
end
disp(['You went to the gym ' num2str(days_gym) ' days and went running ' num2str(days_running) ' days']);
The code is also HERE if you want it with comments.
Let's pull it apart. First, two variables are created - days_running and days_gym - and set to zero. Then, a for loop is used to simulate the 7 days of a week. Now, the rand command. The rand command in this case generates a random number between 0 and 1. Now, the if statement. You can read this literally - if did_it_rain is less than or equal to 0.3 then we increase days_gym. If it is not, we increase days_running. You always have to end if statements just like for loops. Also note, you go not been to have an else if you do not want to.
I would try running this code a few times to make sure you understand what it is doing and to see that days_gym and days_running change each time you run it.
If statements are very powerful - we will be using them a lot more throughout this tutorial.
Note, if you want to check to see if some is equal to something the logical test is not = but ==. So, if you want to check if a counter is equal to something you would use ==. For example:
counter = 0;
while 1
counter = counter + 1;
if counter == 100
break;
end
end
This is actually quite a powerful loop structure. while 1 forces MATLAB into an infinite loop. However, when a criteria is met, in this case counter == 100 then we can use a break command. If you think of video games or experiments this is a very useful loop structure to use.
Additional Acivity.
Use the while 1 loop structure to do something useful. Generate your own code and have some fun.
You can move onto the next tutorial now.
clc;
clear all;
close all;
rng('shuffle');
This last command seeds the random number generator so any generated numbers are truly "random". If you do not use this, you will find that your so called random numbers always follow the same pattern. Now, for the rest of the script.
days_running = 0;
days_gym = 0;
for day_counter = 1:7
did_it_rain = rand(1);
if did_it_rain <= 0.3
days_gym = days_gym + 1;
else
days_running = days_running + 1;
end
end
disp(['You went to the gym ' num2str(days_gym) ' days and went running ' num2str(days_running) ' days']);
The code is also HERE if you want it with comments.
Let's pull it apart. First, two variables are created - days_running and days_gym - and set to zero. Then, a for loop is used to simulate the 7 days of a week. Now, the rand command. The rand command in this case generates a random number between 0 and 1. Now, the if statement. You can read this literally - if did_it_rain is less than or equal to 0.3 then we increase days_gym. If it is not, we increase days_running. You always have to end if statements just like for loops. Also note, you go not been to have an else if you do not want to.
I would try running this code a few times to make sure you understand what it is doing and to see that days_gym and days_running change each time you run it.
If statements are very powerful - we will be using them a lot more throughout this tutorial.
Note, if you want to check to see if some is equal to something the logical test is not = but ==. So, if you want to check if a counter is equal to something you would use ==. For example:
counter = 0;
while 1
counter = counter + 1;
if counter == 100
break;
end
end
This is actually quite a powerful loop structure. while 1 forces MATLAB into an infinite loop. However, when a criteria is met, in this case counter == 100 then we can use a break command. If you think of video games or experiments this is a very useful loop structure to use.
Additional Acivity.
Use the while 1 loop structure to do something useful. Generate your own code and have some fun.
You can move onto the next tutorial now.