Eamonn Keogh eamonn@cs.ucr.edu CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu
Manipulating Arrays We can think of arrays like Lego We can pull them apart and assemble them anyway we want. [1 7 3 2 4 8 5 6] [4 8 3 2]
The Colon Operator EDU>> 1:5 ans = 1 2 3 4 5 EDU>> 1:3 1 2 3 Starting with this number.. Ending with this number.. Return an array with all the ordered integers
We can assign the result of a colon operation to an array variable EDU>> MyNumbers = 10 : 15 MyNumbers = 10 11 12 13 14 15 EDU>> MyNumbers = [10 : 15] The square brackets are optional..
The start and/or end points can be variables EDU>> YearJoeBorn = 2007; EDU>> CurrentYear = 2012; EDU>> EDU>> YearsInWhichJoeLive = YearJoeBorn : CurrentYear YearsInWhichJoeLive = 2007 2008 2009 2010 2011 2012
Array Manipulation: Deleting EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> BoysTeamsAges(2) = []; 21 56 To delete an element in an array, assign it the empty set
Array Manipulation: Assembling EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> BoysTeamsAges = [BoysTeamsAges 77 ] 21 23 56 77
EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> BoysTeamsAges = [44 BoysTeamsAges ] 44 21 23 56 EDU>>
EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> BoysTeamsAges = [44 55 23 BoysTeamsAges ] 44 55 23 21 23 5
EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> BoysTeamsAges = [ [44 55 23] BoysTeamsAges ] 44 55 23 21 23 56 This is logically equivalent to way I did this in the last slide
EDU>> BoysTeamsAges = [21 23 56] BoysTeamsAges = 21 23 56 EDU>> GirlsTeamsAges = [24 29 19] GirlsTeamsAges = 24 29 19 EDU>> MixedTeam = [BoysTeamsAges GirlsTeamsAges ] MixedTeam = 21 23 56 24 29 19
EDU>> BoysTeamsAges = [21] BoysTeamsAges = 21 EDU>> BoysTeamsAges = [BoysTeamsAges 34] 21 34 EDU>> BoysTeamsAges = [BoysTeamsAges 45] 21 34 45
EDU>> BoysTeamsAges = [] BoysTeamsAges = [] EDU>> BoysTeamsAges = [BoysTeamsAges 22] 22 EDU>> BoysTeamsAges = [BoysTeamsAges 42] 22 42
Array Manipulation: Disassembling EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges(2) ans = 29 EDU>> GirlsTeamsAges(end) 76
EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges(2) ans = 29 EDU>> GirlsTeamsAges(end) 76
EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges([1,3]) ans = 24 19 Note the square brackets
EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges([1 3]) ans = 24 19 The comma was optional
EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges([1,2,1,4]) ans = 24 29 24 76 Note that I extracted item 1 twice! This was deliberate and legal
EDU>> GirlsTeamsAges = [24 29 19 76] GirlsTeamsAges = 24 29 19 76 EDU>> GirlsTeamsAges([1,2,1,4]) ans = 24 29 24 76 Note that I extracted item 1 twice! This was deliberate and legal
The Colon Operator Quick Refresher EDU>> 1:5 ans = 1 2 3 4 5 EDU>> 1:3 1 2 3 Quick Refresher Starting with this number.. Ending with this number.. Return an array with all the ordered integers
We can use the colon operator to index arrays EDU>> BoysTeamsAges = [44 55 23 45 45 67 65 ] BoysTeamsAges = 44 55 23 45 45 67 65 EDU>> BoysTeamsAges(1:3) ans = 44 55 23 We know that 1:3 evaluates to [1 2 3], and we know what BoysTeamsAges([1 2 3]) returns
EDU>> BoysTeamsAges = [44 55 23 45 45 67 65 ] BoysTeamsAges = 44 55 23 45 45 67 65 EDU>> BoysTeamsAges(1:3) ans = 44 55 23 EDU>> BoysTeamsAges(3:6) 23 45 45 67
EDU>> BoysTeamsAges = [44 55 23 45 45 67 65 ] BoysTeamsAges = 44 55 23 45 45 67 65 EDU>> BoysTeamsAges(2:end) ans = 55 23 45 45 67 65 EDU>> BoysTeamsAges(1:end-1) 44 55 23 45 45 67
The start /stop values can be variables EDU>> BoysTeamsAges = [44 55 23 45 45 67 65 ]; EDU>> EDU>> fink = 2; EDU>> nottle = 4; EDU>> BoysTeamsAges(fink:nottle) ans = 55 23 45 The start /stop values can be variables
Homework You must fill in this homework before Friday lab. The first time you talk to the TA, he will ask to see it. If you have not done it, the TA will not help you in lab that day.
EDU>> BoysAges = [8 9 8 7 6]; EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [8 9 8 7 6]; EDU>> GirlsAges = [7 6 6 10 9 11]; Fill in the missing code below. Note that if I ask you to find the oldest boy, I want the general code that will work, even if I change the variables above, so EDU>> OldestBoy = max(BoysAges ) % something like this is correct EDU>> OldestBoy = max([8 9 8 7 6]) % no! EDU>> OldestBoy = 9 % no! Get the age of the oldest girl >> OG = max(GirlsAges ) Get the age of the oldest boy >> OB = Get the age of the youngest boy >> Get the age difference between the oldest and youngest boy >> AD = max( Get the first two listed boys FTB = BoysAges(1:2) Get the first three listed girls FTG = Get the number of girls
EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [8 9 8 7 6]; EDU>> GirlsAges = [7 6 6 10 9 11]; Get the first four listed boys FFB = Get the last listed girl Get the last two listed boys Build a vector that has the first boy and the first girl FBFG = [ BoysAges(1) GirlsAges(1) ]; Build a vector that has the last boy and the first girl LBFG = Build a vector that has the last two boys and the last two girls Build a vector that has all the boys and all the girls CoED = [ Find the oldest player (of either sex) Hint, you can use CoED
EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [8 9 8 7 6]; EDU>> GirlsAges = [7 6 6 10 9 11]; Add a new player, age 12, to the end of the boys team BoysAges = [BoysA… Add a new player, aged 6 to the beginning of the girls team GirlsAges = [ Delete the third boy BoysAges(3) = Delete first four girls GirlsAges(
EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [8 9 8 7 6]; EDU>> GirlsAges = [7 6 6 10 9 11]; % fill in the missing code below, guess what is missing by the context if ____________________ disp(‘there are more boys than girls’) end disp(‘the oldest boy is older than 10’) disp(‘the age gap in the girls team is less than 3’) disp(‘the oldest boy is older than the youngest girl’)
if ____________________ EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [8 9 8 7 6]; EDU>> GirlsAges = [7 6 6 10 9 11]; % fill in the missing code below, guess what is missing by the context if ____________________ disp(‘There are more than 4 girls on our team’) end We have a player aged… 8 9 for i = 1 : ________________ % list ages of ALL players disp(‘We have a player aged…’) disp(BoysTeamsAges(i)) Our oldest boy is number 2 In the list if _____________________ disp(‘our oldest boy is number ’) disp(i) disp(‘in the list’)