Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu.

Similar presentations


Presentation on theme: "Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu."— Presentation transcript:

1 Eamonn Keogh eamonn@cs.ucr.edu
CS005 Introduction to Programming: Matlab Eamonn Keogh

2 Review: Arrays (and Matrices)
Recall our view of variables as locations in memory.. If we typed… >> HisAge = 25 HisAge = 25 HisAge 3 inf 25 23 48 Nan 2 -2

3 Review: Arrays (and Matrices)
We can have arrays (vectors) of variables instead If we type… EDU>> KidsAge = [15, 19, 4 11] KidsAge = KidsAge 3 inf 15 19 4 11 2 -2 23

4 3 inf 15 19 4 11 2 -2 23 KidsAge(1) KidsAge
Review: In order to tell matlab which of the four values we are interested in, we must index the variables EDU>> KidsAge(1) ans = 15 EDU>> KidsAge(4) 11 EDU>> KidsAge(2) 19 KidsAge(1) This is the index, or the subscript of the variable KidsAge. We can pronounce it as: KidsAge at 1 Or KidsAge sub 1 KidsAge 3 inf 15 19 4 11 2 -2 23

5 Review: We can use an index to change values. Suppose we realized we had made a mistake, and the second child is really 18… EDU>> KidsAge(2) = 18 KidsAge = KidsAge 3 inf 15 18 4 11 2 -2 23

6 Review: Indexed Variables and Looping
The real power of indexed variables comes from use them with loops (and vice versa) For the rest of the quarter, virtually everything we do will involve these two things.

7 Let us write a function to test if an Array has at least one odd number
12 18 4 11 AtLeastOneOdd 1

8 function HasOdd = AtLeastOneOdd(InArray) for i = 1 : length(InArray) i
end EDU>> testme = [ ] testme = EDU>> AtLeastOneOdd(testme) i = 1 2 3 4 As always, we will build up to the task, starting with the simplest subset of the problem. Let us just count from one to the size of the array…

9 function HasOdd = AtLeastOneOdd(InArray) for i = 1 : length(InArray)
if IsOdd( InArray(i) ) disp('found an odd, when i was ') disp(i) end Let us display to the screen if we find an odd… Works, but we want to return true/false, not display anything (next slide) EDU>> testme = [ ] testme = EDU>> AtLeastOneOdd(testme) found an odd, when i was 3

10 function HasOdd = AtLeastOneOdd(InArray)
HasOdd = 0; % Assume there is no odd for i = 1 : length(InArray) if IsOdd( InArray(i) ) HasOdd = 1; % Change assumption end EDU>> AtLeastOneOdd(testme) ans = 1 EDU>> AtLeastOneOdd([ ]) EDU>> AtLeastOneOdd([ ]) We make an assumption, and give the for loop every chance to change it

11 function HasOdd = AtLeastOneOdd(InArray) % This code is wrong
for i = 1 : length(InArray) if IsOdd( InArray(i) ) HasOdd = 1; elseif HasOdd = 0; end This code is a common mistake, it will return true or false based only on the LAST element in the array.

12 Let us write a function to count the number of odd numbers in an Array
12 7 4 11 NumberOfOdds 2

13 function HasOdd = AtLeastOneOdd(InArray)
HasOdd = 0; % Assume there is no odd for i = 1 : length(InArray) if IsOdd( InArray(i) ) HasOdd = 1; % Change assumption end Start with working related code. function OddCount = NumberOfOdds(InArray) HasOdd = 0; % Assume there is no odd for i = 1 : length(InArray) if IsOdd( InArray(i) ) HasOdd = 1; % Change assumption end Begin by renaming…

14 function OddCount = NumberOfOdds(InArray)
OddCount = 0; % Assume there is no odd, so count is 0 for i = 1 : length(InArray) if IsOdd( InArray(i) ) OddCount = OddCount + 1; % Change assumption end EDU>> NumberOfOdds([ ]) ans = 1 EDU>> NumberOfOdds([ ]) 2 EDU>> NumberOfOdds([ ]) 5

15 Let us write a function to reverse an Array
We have… KidsAge 3 inf 15 18 4 11 2 -2 23 We want… KidsAge 3 inf 11 4 18 15 2 -2 23

16 15 18 4 11 ReverseArray 11 4 18 15 EDU>> KidsAge KidsAge =
EDU>> ReverseArray(KidsAge) ans =

17 15 18 4 11 OutArray 15 18 4 11 15 OutArray 15 18 4 11 18 15 OutArray 15 18 4 11 4 18 15 OutArray

18 15 18 4 11 15 OutArray The length of the array is 4 1 2 3 4 4-4+1 4-3+1 4-2+1 4-1+1

19 function OutArray = ReverseArray(InArray) for i = 1 : length(InArray)
end EDU>> ReverseArray(KidsAge) i = 1 2 3 4 As always, we will build up to the task, starting with the simplest subset of the problem. Let us just count from one to the size of the array…

20 function OutArray = ReverseArray(InArray) for i = 1 : length(InArray)
InArray(i) end EDU>> ReverseArray(KidsAge) ans = 15 18 4 11 Let us list the items in the array…

21 function OutArray = ReverseArray(InArray) for i = 1 : length(InArray)
InArray(i) length(InArray)-i + 1 end EDU>> ReverseArray(KidsAge) ans = 15 4 18 3 2 11 1 First time thru the loop Let us list the items in the array… And count backwards from the size of the array Second time thru the loop Third time thru the loop

22 function OutArray = ReverseArray(InArray) for i = 1 : length(InArray)
OutArray( length(InArray)-i + 1) = InArray(i); end EDU>> KidsAge KidsAge = EDU>> ReverseArray(KidsAge) ans = It works! Here we have not changed KidsAge, to do that (next slide)

23 EDU>> KidsAge KidsAge = EDU>> KidsAge = ReverseArray(KidsAge) To change KidsAge we need to assign the reversed array to KidsAge


Download ppt "Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu."

Similar presentations


Ads by Google