lesson 4: basic algorithm tools

19
Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case

Upload: gavin-chan

Post on 03-Jan-2016

20 views

Category:

Documents


1 download

DESCRIPTION

Lesson 4: Basic Algorithm Tools. If, While Do For Loop, Else, Switch Case. 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). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lesson 4: Basic Algorithm Tools

Lesson 4: Basic Algorithm ToolsIf, While Do For Loop,

Else, Switch Case

Page 2: Lesson 4: Basic Algorithm Tools

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

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

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

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

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

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

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

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

For Loop

Page 11: Lesson 4: Basic Algorithm Tools

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

While Loops

Page 13: Lesson 4: Basic Algorithm Tools

If (condition) Else

If age < 65

ageClass = 'Young'

ageClass = 'Elder'

Page 14: Lesson 4: Basic Algorithm Tools

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-Then-Else Block

Page 16: Lesson 4: Basic Algorithm Tools

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 – ElseIf Block

Page 18: Lesson 4: Basic Algorithm Tools

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

Switch (variable) case...otherwise

switch Age

case 10 armLength = 30

case 15 armLength = 35

case 20 armLength = 40

otherwise armLength = 42