CS 005 Introduction to Programming Matlab Eamonn Keogh
CS 005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs. ucr. edu
Review: Arrays (and Matrices) Recall our view of variables as locations in memory. . If we typed… >> His. Age = 25 His. Age 3 inf 25 23 48 Nan 2 -2 23 23
Review: Arrays (and Matrices) We can have arrays (vectors) of variables instead If we type… EDU>> Kids. Age = [15, 19, 4 11] Kids. Age = 15 19 4 11 Kids. Age 3 inf 15 19 4 11 2 -2 23 23
Review: In order to tell matlab which of the four values we are interested in, we must index the variables EDU>> Kids. Age(1) ans = 15 EDU>> Kids. Age(4) ans = 11 EDU>> Kids. Age(2) ans = 19 Kids. Age(1) This is the index, or the subscript of the variable Kids. Age. We can pronounce it as: Kids. Age at 1 Or Kids. Age sub 1 Kids. Age 3 inf 15 19 4 11 2 -2 23 23
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>> Kids. Age(2) = 18 Kids. Age = 15 18 4 11 Kids. Age 3 inf 15 18 4 11 2 -2 23 23
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.
Let us write a function to test if an Array has at least one odd number 12 18 4 11 At. Least. One. Odd 1
function Has. Odd = At. Least. One. Odd(In. Array) for i = 1 : length(In. Array) i end 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… EDU>> testme = [ 2 8 7 4] testme = 2 8 7 4 EDU>> At. Least. One. Odd(testme) i= 1 i= 2 i= 3 i=
function Has. Odd = At. Least. One. Odd(In. Array) for i = 1 : length(In. Array) if Is. Odd( In. Array(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 = [ 2 8 7 4] testme = 2 8 7 4 EDU>> At. Least. One. Odd(testme) found an odd, when i was 3
function Has. Odd = At. Least. One. Odd(In. Array) Has. Odd = 0; % Assume there is no odd for i = 1 : length(In. Array) if Is. Odd( In. Array(i) ) Has. Odd = 1; % Change assumption end We make an assumption, and give the for loop every chance to change it EDU>> At. Least. One. Odd(testme) ans = 1 EDU>> At. Least. One. Odd([2 4 6 8 10 ]) ans = 0 EDU>> At. Least. One. Odd([2 4 6 7 10 ]) ans = 1
function Has. Odd = At. Least. One. Odd(In. Array) % This code is wrong for i = 1 : length(In. Array) if Is. Odd( In. Array(i) ) Has. Odd = 1; elseif Has. Odd = 0; end This code is a common mistake, it will return true or false based only on the LAST element in the array.
Let us write a function to count the number of odd numbers in an Array 12 7 4 11 Number. Of. Odds 2
function Has. Odd = At. Least. One. Odd(In. Array) Has. Odd = 0; % Assume there is no odd for i = 1 : length(In. Array) Start with working if Is. Odd( In. Array(i) ) related code. Has. Odd = 1; % Change assumption end function Odd. Count = Number. Of. Odds(In. Array) Has. Odd = 0; % Assume there is no odd for i = 1 : length(In. Array) Begin by renaming… if Is. Odd( In. Array(i) ) Has. Odd = 1; % Change assumption end
function Odd. Count = Number. Of. Odds(In. Array) Odd. Count = 0; % Assume there is no odd, so count is 0 for i = 1 : length(In. Array) if Is. Odd( In. Array(i) ) Odd. Count = Odd. Count + 1; % Change assumption end EDU>> Number. Of. Odds([2 4 6 7 10 ]) ans = 1 EDU>> Number. Of. Odds([2 4 6 7 3 ]) ans = 2 EDU>> Number. Of. Odds([1 3 5 7 9 10 8 ]) ans = 5
Let us write a function to reverse an Array We have… Kids. Age 3 inf 15 18 4 11 2 -2 23 23 We want… Kids. Age 3 inf 11 4 18 15
15 18 4 11 Reverse. Array EDU>> Kids. Age = 15 18 4 11 EDU>> Reverse. Array(Kids. Age) ans = 11 4 18 15
15 18 4 11 Out. Array 15 18 4 11 15 Out. Array 15 18 4 11 18 15 Out. Array 15 18 Out. Array 4 4 1 18 1
15 18 4 11 15 Out. Array The length of the array is 4 1 2 3 4 4 -4+1 4 -3+1 4 -2+1 4 -1+1
function Out. Array = Reverse. Array(In. Array) for i = 1 : length(In. Array) i end 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… EDU>> Reverse. Array(Kids. Age) i= 1 i= 2 i= 3 i= 4
function Out. Array = Reverse. Array(In. Array) for i = 1 : length(In. Array) In. Array(i) end Let us list the items in the array… EDU>> Reverse. Array(Kids. Age) ans = 15 ans = 18 ans = 4 ans = 11
function Out. Array = Reverse. Array(In. Array) for i = 1 : length(In. Array) In. Array(i) length(In. Array)-i + 1 end EDU>> Reverse. Array(Kids. Age) Let us list the items in the array… And count backwards from the size of the array ans = 15 ans = 4 ans = 18 ans = 3 ans = 4 ans = 2 ans = First time thru the loop Second time thru the loop Third time thru the loop
function Out. Array = Reverse. Array(In. Array) for i = 1 : length(In. Array) Out. Array( length(In. Array)-i + 1) = In. Array(i); end It works! EDU>> Kids. Age = 15 18 4 11 Here we have not changed Kids. Age, to do that (next slide) EDU>> Reverse. Array(Kids. Age) ans = 11 4 18 15
EDU>> Kids. Age = 15 18 4 11 EDU>> Kids. Age = Reverse. Array(Kids. Age) Kids. Age = 11 4 18 15 To change Kids. Age we need to assign the reversed array to Kids. Age
- Slides: 23