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

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
CS150 Introduction to Computer Science 1
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Matlab Programming for Engineers
Loops CS 103 February 13, 2009 Author: Nate Hamm.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Program design Program Design Process has 2 phases:
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
CS005 Introduction to Programming
Bill Tucker Austin Community College COSC 1315
Matlab Training Session 4: Control, Flow and Functions
Yanal Alahmad Java Workshop Yanal Alahmad
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming:
CS005 Introduction to Programming:
CS005 Introduction to Programming
Week 8 - Programming II Today – more features: Loop control
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
One-Dimensional Array Introduction Lesson xx
List Algorithms Taken from notes by Dr. Neil Moore
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Chapter 7 Multidimensional Arrays
CS005 Introduction to Programming
CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Types of Flow of Control
Coding Concepts (Basics)
CS005 Introduction to Programming: Matlab
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Logical Operations In Matlab.
Faculty of Computer Science & Information System
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lets Play with arrays Singh Tripty
Chapter 7 Multidimensional Arrays
Arrays.
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
Matlab Basics Tutorial
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Introduction to Computer Programming IT-104
REPETITION Why Repetition?
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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 = 15 18 4 11 KidsAge 3 inf 15 18 4 11 2 -2 23

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

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 = 11 7 6

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

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

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 9 4 8 7 5 8 4 6 5 7 7

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

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

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 = 9 4 8 7 5 8 4 6 5 7 7

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

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

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

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 9 4 8 7 5 8 4 6 5 7 7 DisplayShoeInfo