1 / 40

Eamonn Keogh eamonn@cs.ucr

CS005 Introduction to Programming: Matlab. Eamonn Keogh eamonn@cs.ucr.edu. Swapping two numbers. EDU>> HisAge = 25; EDU>> HerAge = 48;. Let us say we have assigned two variables, HisAge and HerAge …

pfry
Download Presentation

Eamonn Keogh eamonn@cs.ucr

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. CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu

  2. Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; Let us say we have assigned two variables, HisAge and HerAge… However we realize that we have made a mistake, we gave the wrong ages to the wrong person…

  3. Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; We can just swap them….

  4. Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; EDU>> HerAge HerAge = 25 EDU>> HisAge HisAge = 25 We can just swap them…. NO! Look at the values…

  5. Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> Temp = HisAge; EDU>> HisAge = HerAge; EDU>> HerAge = Temp; EDU>> EDU>> HisAge HisAge = 48 EDU>> HerAge HerAge = 25 We need a temp variable

  6. DeBlanking a String EDU>> MyPass = 'do geese see god' MyPass = do geese see god EDU>> DeBlank(MyPass) ans = dogeeseseegod EDU>> DeBlank('do geese see god') ans = dogeeseseegod Sometimes it is useful to remove blank spaces from strings… 'do geese see god' DeBlank 'dogeeseseegod'

  7. functionDeBlankedString = DeBlank(S) fori = 1 : length(S) disp(S(i)) end EDU>> DeBlank('do geese see god') d o g e e s e s e e g o d

  8. functionDeBlankedString = DeBlank(S) DeBlankedString = []; fori = 1 : length(S) if S(i) ~= ' ' disp(S(i)); end end EDU>> DeBlank('do geese see god') d o g e e s e s e e g o d

  9. functionDeBlankedString = DeBlank(S) DeBlankedString = []; fori = 1 : length(S) if S(i) ~= ' ' DeBlankedString = [DeBlankedString S(i)]; end end EDU>> DeBlank('do geese see god') dogeeseseegod EDU>> DeBlank('eamonn john keogh') ans = eamonnjohnkeogh

  10. Reversing an Array EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4 Let us write a function that takes in an array, and returns it reversed [ 4, 2, 9, 7] ReverseMe [ 7, 9, 2, 4]

  11. function Reversed = ReverseMe(InputArray) fori = 1 : length(InputArray) disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7

  12. function Reversed = ReverseMe(InputArray) Len = length(InputArray); fori = 1 : Len disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7

  13. function Reversed = ReverseMe(InputArray) Len = length(InputArray); fori = 1 : Len disp( InputArray( Len + 1 - i ) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 7 9 2 4

  14. function Reversed = ReverseMe(InputArray) Len = length(InputArray); fori = 1 : Len Reversed(i) = InputArray( Len + 1 - i ); end EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4

  15. EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4 EDU>> ReverseMe([ 4 ]) ans = 4 EDU>> ReverseMe([ 4 4 inf]) ans = Inf 4 4

  16. Testing for Palindromes Let us write a function that takes in string, and returns true if it is an palindrome, otherwise it returns false EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome(‘eamonn') ans = 0 'radar' Palindrome 1

  17. Testing for Palindromes

  18. functionIsPalindrome = Palindrome(S) SLen = length(S); fori = 1 : SLen disp( S(i) ) end EDU>> Palindrome(‘radar') r a d a r EDU>> Palindrome('eamonn') e a m o n n

  19. functionIsPalindrome = Palindrome(S) SLen = length(S); fori = 1 : SLen disp( S(SLen + 1 - i) ) end EDU>> Palindrome(‘radar') r a d a r EDU>> Palindrome('eamonn') n n o m a e

  20. functionIsPalindrome = Palindrome(S) SLen = length(S); fori = 1 : SLen disp( S(i) ) disp( S(SLen + 1 - i) ) disp('****************') end EDU>> Palindrome(‘radar') r r **************** a a **************** d d **************** a a **************** r r **************** EDU>> Palindrome('eamonn') e n **************** a n **************** ...(deleted for clarity)

  21. functionIsPalindrome = Palindrome(S) SLen = length(S); IsPalindrome = 1; fori = 1 : SLen if S(i) ~= S(SLen + 1 - i) IsPalindrome = 0; end end EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome('eamonn') ans = 0

  22. Our palindrome function works on single word palindromes: radar, level, rotor, kayak, redder, madam • It works on some multiword palindromes: step on no pets • It does not work on most multiword palindromes: a man, a plan, a canal Panama,  Madam, Im Adam, never odd or even , rise to vote sir • We could fix that, with DeBlank()

  23. Last times slides are below for ref

  24. Searching Arrays • Many problems involve searching an array (including a string) for special values. • Examples include • Searching an array for the largest value (which player in my team makes the most money?) • Searching an array for a particular value (do I have a student with the ID 8932328?) • Searching a string for a particular value(s) ( Does this string “control” have vowels? CNTRL)

  25. Searching Arrays • Many problems involve searching an array (including a string) for special values. • These problems are so important that Matlab has special build-in functions to solve some of them. • We have seen: EDU>> max([ 2 3 5 3 1 6 4 3 8]) ans = 8 • However, let us write some of these function from scratch to build our skill set…

  26. Finding Existence of a Given Number EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9; EDU>> EamonnsFindNumber(TeamAges,AgeToFind) ans = 1 EDU>> EamonnsFindNumber(TeamAges,77) ans = 0 EDU>> EamonnsFindNumber([66, 77, 45],77) ans = 1 Let us write a function that takes in an array, a number to find in that array, and returns true/false if the number is in the array [ 4, 2, 9] 2 EamonnsFindNumber 1

  27. functionTargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now fori = 1 : length(ArrayToSearch) ifArrayToSearch(i) == TargetNum TargetNumberExists = 1; % Change our assumption end end ArrayToSearch EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9;

  28. functionTargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now fori = 1 : length(ArrayToSearch) ifArrayToSearch(i) == TargetNum disp(['the if statement was triggered with i equals ', num2str(i)]) TargetNumberExists = 1; % Change our assumption end end Useful hint: It is good idea to add temporary code to echo what is happening in the code. This is especially true as we consider more complex code EDU>> EamonnsFindNumber([66, 77, 45],77) the if statement was triggered with i equals 2 ans = 1

  29. Finding Location of Given Number EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9; EDU>> FindNumberLocation(TeamAges,AgeToFind) ans = 4 EDU>> FindNumberLocation(TeamAges,77) ans = NaN [ 4, 2, 9] 9 Let us write a function that takes in an array, a number to find in that array, and returns the location of that number in the array. If the number is not in the array, it return NaN FindNumberLocation 3

  30. Finding Location of Given Number • Important note: • The previous slide did not specify what to do if the target number appears more than once. • It is our job to clarify these things • We could: • Report the first occurrence • Report the second occurrence • Do something else [ 9, 2, 9] 9 Let us write a function that takes in an array, a number to find in that array, and returns the location of that number in the array. If the number is not in the array, it return NaN FindNumberLocation 3

  31. functionNumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now fori = 1 : length(ArrayToSearch) ifArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location end end EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 3

  32. functionNumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now fori = 1 : length(ArrayToSearch) ifArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location end end Does our code find the first or last occurrence of a number? It finds the last occurrence In the next slide we will see how to find the first instead EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 3

  33. functionNumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now fori = 1 : length(ArrayToSearch) ifArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location break; end end The matlab command break terminates a for or while loop. The moment that Matlab encounters a break it jumps to the end of the loop and continues with the rest of the program… EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 1

  34. Finding Vowel Count Let us write a function that takes in a string, and returns a count of how many vowels it has. Let us use our strategy of solving a simpler problem and working our way upto the final solution… ‘eamonn’ Let us use our strategy of echoing to the screen so we can debug the code.. FindVowels 3

  35. functionVowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels fori = 1 : length(StringToSearch) ifStringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end end EDU>> FindVowels('eamonn') An a was found when i was 2 ans = 1 EDU>> FindVowels('abba') An a was found when i was 1 An a was found when i was 4 ans = 2

  36. functionVowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels fori = 1 : length(StringToSearch) ifStringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end end There is a bug! We are not finding uppercase ‘A’ EDU>> FindVowels('ABBA') ans = 0

  37. function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if lower(StringToSearch(i)) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end end There is a simple fix EDU>> FindVowels('ABBA') An a was found when i was 1 An a was found when i was 4 ans = 2

  38. functionVowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels fori = 1 : length(StringToSearch) if lower(StringToSearch(i)) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end if lower(StringToSearch(i)) == 'e' VowelCount = VowelCount + 1; disp(['An e was found when i was ', num2str(i)]) end end EDU>> FindVowels('eamonn') An e was found when i was 1 An a was found when i was 2 ans = 2 We can begin to add more code..

  39. Finding The Largest Value Let us write a function that finds the largest number in an array. As it happens, the built in max function does this, but we need to sharpen our skills… [ 1 3 4 5] EamonnsMax 5

  40. functionLargestNumber = EamonnsMax(InputArray) LargestNumber = -Inf; % Assume this is the largest number fori = 1 : length(InputArray) ifInputArray(i) > LargestNumber disp('I need to update the largest number') LargestNumber = InputArray(i); end end EDU>> EamonnsMax([1 4 2 8 4]) I need to update the largest number I need to update the largest number I need to update the largest number ans = 8 EDU>> EamonnsMax([4 3 2 1]) I need to update the largest number ans = 4

More Related