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 Midterm Mean was 26 (84.1%) Max was 31 (100%) Min was 13 (41.9%) D C B
40 50 60 70 80 90 100 D C B A

3 function WasOdd = IsOdd(IntegerToTest)
Review: Last time we wrote a function that decides if a number was odd function WasOdd = IsOdd(IntegerToTest) if IntegerToTest/2 > floor(IntegerToTest/2) WasOdd = 1; else WasOdd = 0; end

4 We can repeat a code block multiple times with the for statement
for index = values do these statements end Review: Last time we saw how to use loops

5 for i = 1 : 10 do these statements end Start with i at this value
Stop when i hits this value for i = 1 : 10 do these statements end Make i one larger when you get here

6 function DummyVar = CountToTen() for i = 1 : 10 disp(i) end
EDU>> CountToTen 1 2 3 4 5 6 7 8 9 10 EDU>>

7 function DummyVar = CountToTenOdd() for i = 1 : 10 if IsOdd(i)
disp(i); end EDU>> CountToTenOdd 1 3 5 7 9 EDU>>

8 Homework: Not collected or graded
Modify the code so that: It prints out the even numbers from 1 to 10 It prints out the odd numbers starting from start to stop >> start = 10; >> stop = 60; >> SuesCountingFunction(start,stop) It prints out the Olympic years from 1896 to 2020 function DummyVar = CountToTenOdd() for i = 1 : 10 if IsOdd(i) disp(i); end

9 Arrays (and Matrices) 48 3 inf 25 23 2 -2 Nan HisAge
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

10 Arrays (and Matrices) 3 inf 15 19 4 11 2 -2 23 KidsAge
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

11 3 inf 15 19 4 11 2 -2 23 KidsAge(1) KidsAge
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

12 We can use an index to change values
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

13 3 inf 11 19 4 -2 23 3 inf 11 19 4 7 23 KidsShoeSize KidsShoeSize
We can build arrays incrementally… EDU>> KidsShoeSize(1) = 11; KidsShoeSize 3 inf 11 19 4 -2 23 EDU>> KidsShoeSize(2) = 7; KidsShoeSize 3 inf 11 19 4 7 23

14 3 inf 11 19 4 7 6 23 KidsShoeSize We can build arrays incrementally...
EDU>> KidsShoeSize(3) = 6; KidsShoeSize 3 inf 11 19 4 7 6 23 Contrast with batch creation EDU>> KidsShoeSize = [11,7,6] KidsShoeSize =

15 We can use indexed variables just like normal variables
EDU>> max( KidsShoeSize(3), 2 ) ans = 6 EDU>> DanceShoeSize = KidsShoeSize(1) + 1 DanceShoeSize = 12 EDU>> bob = mist( KidsShoeSize(1), 99); KidsShoeSize 3 inf 11 19 4 7 6 23

16 We can use indexed variables just like normal variables
EDU>> KidsShoeSize(1) = GetShoeSizeSpanish() Lo que el tamaño del zapato toma usted? : 4 KidsShoeSize = KidsShoeSize 3 inf 11 19 4 7 6 23

17 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 start by writing a program that gets the shoe sizes of every player on a soccer team… GetTeamsShoeSizes ArrayOfShoeSizes

18 function ArrayOfShoeSizes = GetTeamsShoeSizes() for i = 1 : 11
disp('Player') disp(i) end EDU>> GetTeamsShoeSizes Player 1 2 3 4 As always, we will build up to the task, starting with the simplest subset of the problem

19 function ArrayOfShoeSizes = GetTeamsShoeSizes() for i = 1 : 11
disp('Player') disp(i) OneShoeSize = input(' Enter your shoe size : '); end EDU>> GetTeamsShoeSizes Player 1 Enter your shoe size : 9 2 Enter your shoe size : 4

20 function ArrayOfShoeSizes = GetTeamsShoeSizes() for i = 1 : 11
disp('Player') disp(i) OneShoeSize = input(' Enter your shoe size : '); ArrayOfShoeSizes(i) = OneShoeSize; end EDU>> TeamBrazil = GetTeamsShoeSizes Player 1 Enter your shoe size : 9 2 Enter your shoe size : 4 ::::::: 11 Enter your shoe size : 7 ans =

21 3 inf 11 19 4 9 8 7 TeamBrazil EDU>> TeamBrazil TeamBrazil =
EDU>> EDU>> TeamBrazil(4) ans = 7 EDU>> TeamBrazil(2) 4 EDU>> TeamBrazil(end) TeamBrazil 3 inf 11 19 4 9 8 7

22 EDU>> sum(TeamBrazil)
ans = 70 EDU>> mean(TeamBrazil) 6.3636 EDU>> max(TeamBrazil) 9 EDU>> min(TeamBrazil) 4 EDU>> length(TeamBrazil) 11 Matlab has many built-in functions that operate on arrays. sum: sums up all the numbers in the array. mean: returns the mean of the array. max: returns the largest value in the array. mean: returns the smallest value in the array. length: returns the length of the array. TeamBrazil 3 inf 11 19 4 9 8 7

23 EDU>> mean(TeamBrazil)
ans = 6.3636 EDU>> sum(TeamBrazil)/length(TeamBrazil) Matlab has many built-in functions that operate on arrays. TeamBrazil 3 inf 11 19 4 9 8 7

24 function DummyVar = DisplayShoeInfo(ArrayOfShoeSizes)
disp('The largest shoe size is '); disp(max(ArrayOfShoeSizes)); disp('The average shoe size is '); disp(mean(ArrayOfShoeSizes)); end We can pass an array into a function… EDU>> DisplayShoeInfo(TeamBrazil) The largest shoe size is 9 The average shoe size is 6.3636 EDU>> DisplayShoeInfo(TeamIreland) 11 7.0034 DisplayShoeInfo


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

Similar presentations


Ads by Google