1 / 44

CSC115: Matlab Special Session

CSC115: Matlab Special Session. Dr. Zhen Jiang Computer Science Department West Chester University. Control Flow If/else For/while. Selection (Decision). Rolling a dice. Sample execution (click on this link to try) Each button in the above sample has. No. Yes. Win/Lost?.

jerica
Télécharger la présentation

CSC115: Matlab Special Session

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. CSC115: Matlab Special Session Dr. Zhen Jiang Computer Science Department West Chester University

  2. Control Flow • If/else • For/while

  3. Selection (Decision) • Rolling a dice. • Sample execution (click on this link to try) • Each button in the above sample has

  4. No Yes Win/Lost? Double the money Bankrupt Restart

  5. If else statement if (condition) action 1 (statements 1) else action 2 (statements 2) end action 3 (statement 3)

  6. No Yes Condition Action 1 Action 2 Action 3

  7. Condition • Simple condition • Format <Value> <relational operators> <Value> • Number value relational operators, Table 10-1, page 189 ==, ~=, <, >, <=, >= !!! Number one error: “(a=2)” instead of “(a==2)”

  8. Condition • Complex condition • &&, ||, ~ (Table 10-2, page 192) • Truth table • Precedence order, table10-3, page 193

  9. Development Process • Identify two exclusive options • Implement each handling in different action parts • Identify the situation (values) for option selection • Make a condition so that all the situation value for option part 1 will lead to this condition true. • Verify all the situation value for option part 2 will lead to this condition false, otherwise, revise the above condition!

  10. If statement, • Multiple selection • Nested if • If else if, page 208 • Switch, page 210 • Example: letter grade

  11. Comments: • Nested if for multiple selection problem If case 1 Else if case 2 else … end %of case 2 if end %of case 1 if

  12. Loop • Price is right. • Sample execution (click on this link to try) • Each button in the above sample REPEAT …?

  13. For loop • Format, page 200. • Logic. • Sample, page 201.

  14. While loop • Format & Logic, page 206 • Sample.

  15. Development process

  16. Exercise 1+2+4+8+... 1+2+3+4+...+99

  17. List client number, client name, street, for all clients that live in zip code “80336” • Example_column

  18. Display all words (or all numbers)

  19. Handles both numbers and words • List client number, client name, street and zip code (Number!!), for all clients that live in zip code “80336” • Example_column

  20. ?

  21. You have to convert number to string (word) format (in order to display together in one line) – num2str(zipNum) • One zip is converted each time, forcing the program handle the display line by line.

  22. What do you see, for the program in that box, to repeat many times? • Not display • Insert each line to result, ready for display • Verify the repetition body: • What the computer do in the first round? • What the computer do in the second round? • What the computer do in the third round? • result = [result;…, num2str(zip(line))]

  23. Repetition body (continue) • Is it optional? • What the value to determine the selected case? • Can you make the condition? • If(zip == 80336) • Values verification • True • False

  24. For loop • Do we know how many times to repeat? • 211 is equal to 1  10 • Initialization • How we insert the first line when we do: result = [result;…, num2str(zip(line))] • result = [ ] • Final touch! • For i=1:10 =>zip(1), zip(2), zip(3)… • num2str(zip(i))

  25. Begin with, end with and found

  26. ?

  27. You have to check the match of each string (word), not the whole array of string – regexp (or strfind) • Using regexp • Not giving you the line number, giving you some results strange. • Need to check if the result is empty. Otherwise, it is selected record. • One check each time, forcing the program handle the display line by line.

  28. What do you see, for the program in that box, to repeat many times? • Not display • Insert line number to result, ready for further use • Verify the repetition body: • What the computer do in the first round? • What the computer do in the second round? • What the computer do in the third round? • result = [result; linenumber]

  29. Repetition body (continue) • Is it optional? • What the value to determine the selected case? • Can you make the condition? • If(regexp/strfind matched) • Regexp(string, ‘*6’, ‘end’) • Strfind(string, ‘H7’) • Cannot support “strfind (string, ‘*H7’)” • Values verification • True • False

  30. For loop • Do we know how many times to repeat? • 211 is equal to 1  10 • Initialization • How we insert the first line when we do: result = [result;linenumber] • result = [ ] • Final touch! • For i=1:10 =>1, 2, 3, … • i

  31. Handles top value • For recruiter “24”, list the top 3 clients (client number, client name, amount paid) who have the highest amount paid • Sort in the descending order by amount paid • Find the original index/position that whose record meeting the simple criteria of “recruiter 24”. • Prepare the display data • If the criteria is not simple, find the selected record with if check during the data preparation process. • Example_sort

  32. ?

  33. You have to convert number to string (word) format (in order to display together in one line) – num2str(amount) • One is converted each time, forcing the program handle the display line by line.

  34. What do you see, for the program in that box, to repeat many times? • Not display • Insert each line to result, ready for display • Verify the repetition body: • What the computer do in the first round? • What the computer do in the second round? • What the computer do in the third round? • result = [result; …, num2str(amount(r))]

  35. Repetition body (continue) • Is it optional? • If yes, what the value to determine the selected case, and the corresponding condition? • This is for a complex criteria to select a line record.

  36. For loop • Do we know how many times to repeat? • Top 3! • Initialization • How we insert the first line when we do: result = [result;…, num2str(amount(r)] • result = [ ] • Final touch! • For i=1:3 =>amount(r(1)), amount(r(2)), … • num2str(amount(r(i))

More Related