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 Manipulating Arrays We can think of arrays like Lego
We can pull them apart and assemble them anyway we want. [ ] [ ]

3 The Colon Operator EDU>> 1:5 ans = EDU>> 1: Starting with this number.. Ending with this number.. Return an array with all the ordered integers

4 We can assign the result of a colon operation to an array variable
EDU>> MyNumbers = 10 : 15 MyNumbers = EDU>> MyNumbers = [10 : 15] The square brackets are optional..

5 The start and/or end points can be variables
EDU>> YearJoeBorn = 2007; EDU>> CurrentYear = 2012; EDU>> EDU>> YearsInWhichJoeLive = YearJoeBorn : CurrentYear YearsInWhichJoeLive =

6 Array Manipulation: Deleting
EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges(2) = []; 21 56 To delete an element in an array, assign it the empty set

7 Array Manipulation: Assembling
EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges = [BoysTeamsAges 77 ]

8 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges = [44 BoysTeamsAges ] EDU>>

9 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges = [ BoysTeamsAges ]

10 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges = [ [ ] BoysTeamsAges ] This is logically equivalent to way I did this in the last slide

11 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> MixedTeam = [BoysTeamsAges GirlsTeamsAges ] MixedTeam =

12 EDU>> BoysTeamsAges = [21] BoysTeamsAges = 21 EDU>> BoysTeamsAges = [BoysTeamsAges 34] EDU>> BoysTeamsAges = [BoysTeamsAges 45]

13 EDU>> BoysTeamsAges = [] BoysTeamsAges = [] EDU>> BoysTeamsAges = [BoysTeamsAges 22] 22 EDU>> BoysTeamsAges = [BoysTeamsAges 42] 22 42

14 Array Manipulation: Disassembling
EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges(2) ans = 29 EDU>> GirlsTeamsAges(end) 76

15 EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges(2) ans = 29 EDU>> GirlsTeamsAges(end) 76

16 EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges([1,3]) ans = 24 19 Note the square brackets

17 EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges([1 3]) ans = 24 19 The comma was optional

18 EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges([1,2,1,4]) ans = Note that I extracted item 1 twice! This was deliberate and legal

19 EDU>> GirlsTeamsAges = [ ] GirlsTeamsAges = EDU>> GirlsTeamsAges([1,2,1,4]) ans = Note that I extracted item 1 twice! This was deliberate and legal

20 The Colon Operator Quick Refresher
EDU>> 1:5 ans = EDU>> 1: Quick Refresher Starting with this number.. Ending with this number.. Return an array with all the ordered integers

21 We can use the colon operator to index arrays
EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges(1:3) ans = We know that 1:3 evaluates to [1 2 3], and we know what BoysTeamsAges([1 2 3]) returns

22 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges(1:3) ans = EDU>> BoysTeamsAges(3:6)

23 EDU>> BoysTeamsAges = [ ] BoysTeamsAges = EDU>> BoysTeamsAges(2:end) ans = EDU>> BoysTeamsAges(1:end-1)

24 The start /stop values can be variables
EDU>> BoysTeamsAges = [ ]; EDU>> EDU>> fink = 2; EDU>> nottle = 4; EDU>> BoysTeamsAges(fink:nottle) ans = The start /stop values can be variables

25 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.

26 EDU>> BoysAges = [8 9 8 7 6];
EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [ ]; EDU>> GirlsAges = [ ]; 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([ ]) % no! EDU>> OldestBoy = % 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

27 EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [ ]; EDU>> GirlsAges = [ ]; 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

28 EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [ ]; EDU>> GirlsAges = [ ]; 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(

29 EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [ ]; EDU>> GirlsAges = [ ]; % 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’)

30 if ____________________
EDU>> % Assume for these problems the following have been defined EDU>> BoysAges = [ ]; EDU>> GirlsAges = [ ]; % 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’)


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

Similar presentations


Ads by Google