Eamonn Keogh eamonn@cs.ucr.edu 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… However we realize that we have made a mistake, we gave the wrong ages to the wrong person…
Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; We can just swap them….
Swapping two numbers NO! EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; EDU>> HerAge HerAge = 25 EDU>> HisAge HisAge = We can just swap them…. NO! Look at the values…
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
DeBlanking a String 'do geese see god' 'dogeeseseegod' EDU>> MyPass = 'do geese see god' MyPass = do geese see god EDU>> DeBlank(MyPass) ans = dogeeseseegod EDU>> DeBlank('do geese see god') Sometimes it is useful to remove blank spaces from strings… 'do geese see god' DeBlank 'dogeeseseegod'
function DeBlankedString = DeBlank(S) for i = 1 : length(S) disp(S(i)) end EDU>> DeBlank('do geese see god') d o g e s
function DeBlankedString = DeBlank(S) DeBlankedString = []; for i = 1 : length(S) if S(i) ~= ' ' disp(S(i)); end EDU>> DeBlank('do geese see god') d o g e s
function DeBlankedString = DeBlank(S) DeBlankedString = []; for i = 1 : length(S) if S(i) ~= ' ' DeBlankedString = [DeBlankedString S(i)]; end EDU>> DeBlank('do geese see god') dogeeseseegod EDU>> DeBlank('eamonn john keogh') ans = eamonnjohnkeogh
Reversing an Array [ 4, 2, 9, 7] [ 7, 9, 2, 4] 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]
function Reversed = ReverseMe(InputArray) for i = 1 : length(InputArray) disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7
function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7
function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len disp( InputArray( Len + 1 - i ) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 7 9 2 4
function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len Reversed(i) = InputArray( Len + 1 - i ); end EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4
EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4 EDU>> ReverseMe([ 4 ]) 4 EDU>> ReverseMe([ 4 4 inf]) Inf 4 4
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') 'radar' Palindrome 1
Testing for Palindromes k e o g h
function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(i) ) end EDU>> Palindrome(‘radar') r a d EDU>> Palindrome('eamonn') e a m o n
function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(SLen + 1 - i) ) end EDU>> Palindrome(‘radar') r a d EDU>> Palindrome('eamonn') n o m a e
function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(i) ) disp( S(SLen + 1 - i) ) disp('****************') end EDU>> Palindrome(‘radar') r **************** a d EDU>> Palindrome('eamonn') e n **************** a ...(deleted for clarity)
function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen if S(i) ~= S(SLen + 1 - i) IsPalindrome = 0; end EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome('eamonn')
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()
Last times slides are below for ref
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)
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…
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) EDU>> EamonnsFindNumber([66, 77, 45],77) 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
function TargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum TargetNumberExists = 1; % Change our assumption end ArrayToSearch 5 6 11 9 8 EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9;
TargetNumberExists = 0; % Assume the number does not exist for now function TargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum disp(['the if statement was triggered with i equals ', num2str(i)]) TargetNumberExists = 1; % Change our assumption 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
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) 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
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
NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location end EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 3
NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location 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
NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location break; 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
Finding Vowel Count ‘eamonn’ 3 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
function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if StringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) 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 2
We are not finding uppercase ‘A’ function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if StringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end There is a bug! We are not finding uppercase ‘A’ EDU>> FindVowels('ABBA') ans =
There is a simple fix 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 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
We can begin to add more code.. 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 if lower(StringToSearch(i)) == 'e' disp(['An e was found when i was ', num2str(i)]) 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..
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
1 4 2 8 function LargestNumber = EamonnsMax(InputArray) LargestNumber = -Inf; % Assume this is the largest number for i = 1 : length(InputArray) if InputArray(i) > LargestNumber disp('I need to update the largest number') LargestNumber = InputArray(i); end EDU>> EamonnsMax([1 4 2 8 4]) I need to update the largest number ans = 8 EDU>> EamonnsMax([4 3 2 1]) 4 1 4 2 8