1 / 19

Lesson 4: Basic Algorithm Tools

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).

gavin-chan
Télécharger la présentation

Lesson 4: Basic Algorithm Tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

  3. 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)

  4. 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.

  5. 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

  6. Playing With Conditional Operators In your command window, type: A = 34 B = 76 C = ‘hello’ D = ‘Hello’ Now, try these examples: A < B A > B A == B A ~= B isequal(C,D) ~false true & false

  7. 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

  8. Loops Why 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.         

  9. For i=1:length(infiles) Is i < length(infiles)? Execute Loop Code i=i+1 Exit Loop Move on  in code.

  10. For Loop

  11. while(condition) Is counter < length & person on balance? Execute Loop Instructions Counter = counter + 1 Exit Loop Move on in code

  12. While Loops

  13. If (condition) Else If age < 65 ageClass = 'Elder' ageClass = 'Young'

  14. If (condition) Else If(condition) Else If RTAvg <= 350 Group = 1 Else if RTAvg <= 600 Group = 3 Group = 2

  15. If-Then-Else Block

  16. If(condition) elseif(condition)...else If Accuracy < 50 Grade = 0 ElseIf Accuracy < 60 Grade = 1 ElseIf Accuracy < 70 Grade = 2 Grade = 3

  17. If – ElseIf Block

  18. 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

  19. Switch (variable) case...otherwise switch Age armLength = 30 case 10 armLength = 35 case 15 armLength = 40 case 20 armLength = 42 otherwise

More Related