Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.
-
date post
19-Dec-2015 -
Category
Documents
-
view
225 -
download
2
Transcript of Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.
![Page 1: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/1.jpg)
Lesson 4: Basic Algorithm ToolsIf, While Do For Loop,
Else, Switch Case
![Page 2: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/2.jpg)
Today's Lesson
Basic Algorithm Tools What's an algorithm? What good are they? Simple Functions Logic Operators Loops (while, for) Logic (if/then, switch/case)
![Page 3: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/3.jpg)
Okay, so what's an Algorithm? Informally: A set of steps to get you from State A to State B.
Formally: A type of effective method in which a list of well-defined instructions for completing a task will, when given an initial state, proceed through a well-defined series of successive states, eventually terminating in an end-state. Informal Example: Read in my Data, Load the middle 10 columns of data, Save those columns to a file (.csv, .tsv,.txt)
![Page 4: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/4.jpg)
So what Good is an Algorithm?
All programs are Algorithms.
Sketching out an informal Algorithm will help you write actual programs.
Breaking your thinking into steps can assist in checking your program for errors.
![Page 5: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/5.jpg)
What do I need to start?
A good mental image of what you want to accomplish.
What is the input? (i.e. your data) What output do you want? (i.e. a subset of your data)
Some basic tools: Very simple Matlab formulas Conditional Operators Loops Conditional statements
![Page 6: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/6.jpg)
Playing With Conditional OperatorsIn your command window, type:A = 34B = 76C = ‘hello’D = ‘Hello’
Now, try these examples:A < BA > BA == BA ~= Bisequal(C,D)~falsetrue & false
![Page 7: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/7.jpg)
The Situation
• Just finished an experiment– Subject walked a line while responding to stimuli– Known data, Age, Accuracy, RT, On Balance
• Need for all subjects prior to losing their balance.– Their grouping, based on Reaction Time– Their grade, based on their accuracy
![Page 8: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/8.jpg)
LoopsWhy use loops? Automatically repeat instructions for a large data set While Loops: Repeat instructions while a particular condition is true. For Loops: Repeat instructions for a particular number of times.
![Page 9: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/9.jpg)
For i=1:length(infiles)
Is i < length(infiles)?
Execute Loop Code
i=i+1
Exit LoopMove on in code.
![Page 10: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/10.jpg)
For Loop
![Page 11: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/11.jpg)
while(condition)
Is counter < length & person on balance?
Execute Loop Instructions
Counter = counter + 1
Exit LoopMove on incode
![Page 12: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/12.jpg)
While Loops
![Page 13: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/13.jpg)
If (condition) Else
If age < 65
ageClass = 'Young'
ageClass = 'Elder'
![Page 14: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/14.jpg)
If (condition) Else If(condition) Else
If RTAvg <= 350
Group = 1 Else if RTAvg <= 600
Group = 2 Group = 3
![Page 15: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/15.jpg)
If-Then-Else Block
![Page 16: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/16.jpg)
If(condition) elseif(condition)...else
If Accuracy < 50
ElseIf Accuracy < 60
Grade = 0
Grade = 1
ElseIf Accuracy < 70
Grade = 2
Grade = 3
![Page 17: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/17.jpg)
If – ElseIf Block
![Page 18: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/18.jpg)
Switch and Case%Set an arm length value dependent on subject age
switch subjectAge case 10 armLength = 30; case 15 armLength = 35; case 20 armLength = 40; otherwise armLength = 42;end
![Page 19: Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.](https://reader030.fdocuments.us/reader030/viewer/2022032800/56649d375503460f94a0fa60/html5/thumbnails/19.jpg)
Switch (variable) case...otherwise
switch Age
case 10 armLength = 30
case 15 armLength = 35
case 20 armLength = 40
otherwise armLength = 42