Download presentation
Presentation is loading. Please wait.
Published byAlicia Kennedy Modified over 6 years ago
1
Eamonn Keogh eamonn@cs.ucr.edu
CS005 Introduction to Programming: Matlab Eamonn Keogh
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 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…
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 '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'
7
function DeBlankedString = DeBlank(S) for i = 1 : length(S) disp(S(i))
end EDU>> DeBlank('do geese see god') d o g e s
8
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
9
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
10
Reversing an Array [ 4, 2, 9, 7] [ 7, 9, 2, 4]
EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 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)
for i = 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);
for i = 1 : Len disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7
13
function Reversed = ReverseMe(InputArray) Len = length(InputArray);
for i = 1 : Len disp( InputArray( Len i ) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 7 9 2 4
14
function Reversed = ReverseMe(InputArray) Len = length(InputArray);
for i = 1 : Len Reversed(i) = InputArray( Len i ); end EDU>> ReverseMe([ 4, 2, 9, 7]) ans =
15
EDU>> ReverseMe([ 4, 2, 9, 7])
ans = EDU>> ReverseMe([ 4 ]) 4 EDU>> ReverseMe([ inf]) Inf
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') 'radar' Palindrome 1
17
Testing for Palindromes
k e o g h
18
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
19
function IsPalindrome = Palindrome(S) SLen = length(S);
for i = 1 : SLen disp( S(SLen i) ) end EDU>> Palindrome(‘radar') r a d EDU>> Palindrome('eamonn') n o m a e
20
function IsPalindrome = Palindrome(S) SLen = length(S);
for i = 1 : SLen disp( S(i) ) disp( S(SLen i) ) disp('****************') end EDU>> Palindrome(‘radar') r **************** a d EDU>> Palindrome('eamonn') e n **************** a ...(deleted for clarity)
21
function IsPalindrome = Palindrome(S) SLen = length(S);
for i = 1 : SLen if S(i) ~= S(SLen i) IsPalindrome = 0; end EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome('eamonn')
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 ?) 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([ ]) 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) 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
27
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;
28
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
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) 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
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
32
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
33
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
34
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
35
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
36
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 =
37
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
38
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..
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… [ ] EamonnsMax 5
40
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([ ]) I need to update the largest number ans = 8 EDU>> EamonnsMax([ ]) 4 1 4 2 8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.