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

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Lecture 4: Looping Tami Meredith. Back to Basics... Programming requires: 1. Identification of the problem. 2. Solving the problem: A. Selecting the data.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CS 106A, Lecture 27 Final Exam Review 1
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Recursion Version 1.0.
Recursive Algorithm R ecursive Algorithm.
String Manipulation Part 1
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
REPETITION CONTROL STRUCTURE
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Correctness issues and Loop invariants
CS005 Introduction to Programming
Introduction To Repetition The for loop
Repetition Structures Chapter 9
CS005 Introduction to Programming
What to do when a test fails
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Debugging and Random Numbers
CS005 Introduction to Programming:
CS005 Introduction to Programming:
Algorithm Analysis CSE 2011 Winter September 2018.
CS005 Introduction to Programming
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Siti Nurbaya Ismail Senior Lecturer
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Some Basics for Problem Analysis and Solutions
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Project 2 datasets are now online.
CS005 Introduction to Programming
File Handling Programming Guides.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Lesson 16: Functions with Return Values
CS005 Introduction to Programming
Advanced Java Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
The datasets have 100 instances, and 100 features
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to TouchDevelop
Variables Title slide variables.
CS005 Introduction to Programming: Matlab
CSC 221: Introduction to Programming Fall 2018
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
What's wrong with Easter jokes? They crack you up
= 4 7 x = 4 27 x = x How many of you would agree with these statements? And you can be honest, I’m a recorded voice, I can’t really see you. Is multiplying.
Project 2, The Final Project
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.
Chapter 9: More About Data, Arrays, and Files
Introduction to Computer Science
Making decisions with code
EE 194/BIO 196: Modeling biological systems
Lec 21 More Fun with Arrays: For Loops
Introduction to Computer Science
Presentation transcript:

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

Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; Let us say we have assigned two variables, HisAge and HerAge… However we realize that we have made a mistake, we gave the wrong ages to the wrong person…

Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; We can just swap them….

Swapping two numbers NO! EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> HerAge = HisAge; EDU>> HisAge = HerAge; EDU>> HerAge HerAge = 25 EDU>> HisAge HisAge = We can just swap them…. NO! Look at the values…

Swapping two numbers EDU>> HisAge = 25; EDU>> HerAge = 48; EDU>> Temp = HisAge; EDU>> HisAge = HerAge; EDU>> HerAge = Temp; EDU>> EDU>> HisAge HisAge = 48 EDU>> HerAge HerAge = 25 We need a temp variable

DeBlanking a String 'do geese see god' 'dogeeseseegod' EDU>> MyPass = 'do geese see god' MyPass = do geese see god EDU>> DeBlank(MyPass) ans = dogeeseseegod EDU>> DeBlank('do geese see god') Sometimes it is useful to remove blank spaces from strings… 'do geese see god' DeBlank 'dogeeseseegod'

function DeBlankedString = DeBlank(S) for i = 1 : length(S) disp(S(i)) end EDU>> DeBlank('do geese see god') d o g e s

function DeBlankedString = DeBlank(S) DeBlankedString = []; for i = 1 : length(S) if S(i) ~= ' ' disp(S(i)); end EDU>> DeBlank('do geese see god') d o g e s

function DeBlankedString = DeBlank(S) DeBlankedString = []; for i = 1 : length(S) if S(i) ~= ' ' DeBlankedString = [DeBlankedString S(i)]; end EDU>> DeBlank('do geese see god') dogeeseseegod EDU>> DeBlank('eamonn john keogh') ans = eamonnjohnkeogh

Reversing an Array [ 4, 2, 9, 7] [ 7, 9, 2, 4] EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4 Let us write a function that takes in an array, and returns it reversed [ 4, 2, 9, 7] ReverseMe [ 7, 9, 2, 4]

function Reversed = ReverseMe(InputArray) for i = 1 : length(InputArray) disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7

function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len disp( InputArray(i) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 4 2 9 7

function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len disp( InputArray( Len + 1 - i ) ); end EDU>> ReverseMe([ 4, 2, 9, 7]) 7 9 2 4

function Reversed = ReverseMe(InputArray) Len = length(InputArray); for i = 1 : Len Reversed(i) = InputArray( Len + 1 - i ); end EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4

EDU>> ReverseMe([ 4, 2, 9, 7]) ans = 7 9 2 4 EDU>> ReverseMe([ 4 ]) 4 EDU>> ReverseMe([ 4 4 inf]) Inf 4 4

Testing for Palindromes Let us write a function that takes in string, and returns true if it is an palindrome, otherwise it returns false EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome(‘eamonn') 'radar' Palindrome 1

Testing for Palindromes k e o g h

function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(i) ) end EDU>> Palindrome(‘radar') r a d EDU>> Palindrome('eamonn') e a m o n

function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(SLen + 1 - i) ) end EDU>> Palindrome(‘radar') r a d EDU>> Palindrome('eamonn') n o m a e

function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen disp( S(i) ) disp( S(SLen + 1 - i) ) disp('****************') end EDU>> Palindrome(‘radar') r **************** a d EDU>> Palindrome('eamonn') e n **************** a ...(deleted for clarity)

function IsPalindrome = Palindrome(S) SLen = length(S); for i = 1 : SLen if S(i) ~= S(SLen + 1 - i) IsPalindrome = 0; end EDU>> Palindrome('radar') ans = 1 EDU>> Palindrome('eamonn')

Our palindrome function works on single word palindromes: radar, level, rotor, kayak, redder, madam It works on some multiword palindromes: step on no pets It does not work on most multiword palindromes: a man, a plan, a canal Panama,  Madam, Im Adam, never odd or even , rise to vote sir  We could fix that, with DeBlank()

Last times slides are below for ref

Searching Arrays Many problems involve searching an array (including a string) for special values. Examples include Searching an array for the largest value (which player in my team makes the most money?) Searching an array for a particular value (do I have a student with the ID 8932328?) Searching a string for a particular value(s) ( Does this string “control” have vowels? CNTRL)

Searching Arrays Many problems involve searching an array (including a string) for special values. These problems are so important that Matlab has special build-in functions to solve some of them. We have seen: EDU>> max([ 2 3 5 3 1 6 4 3 8]) ans = 8 However, let us write some of these function from scratch to build our skill set…

Finding Existence of a Given Number EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9; EDU>> EamonnsFindNumber(TeamAges,AgeToFind) ans = 1 EDU>> EamonnsFindNumber(TeamAges,77) EDU>> EamonnsFindNumber([66, 77, 45],77) Let us write a function that takes in an array, a number to find in that array, and returns true/false if the number is in the array [ 4, 2, 9] 2 EamonnsFindNumber 1

function TargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum TargetNumberExists = 1; % Change our assumption end ArrayToSearch 5 6 11 9 8 EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9;

TargetNumberExists = 0; % Assume the number does not exist for now function TargetNumberExists = EamonnsFindNumber(ArrayToSearch,TargetNum) TargetNumberExists = 0; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum disp(['the if statement was triggered with i equals ', num2str(i)]) TargetNumberExists = 1; % Change our assumption end Useful hint: It is good idea to add temporary code to echo what is happening in the code. This is especially true as we consider more complex code EDU>> EamonnsFindNumber([66, 77, 45],77) the if statement was triggered with i equals 2 ans = 1

Finding Location of Given Number EDU>> TeamAges = [ 5 , 6, 11, 9 , 8]; EDU>> AgeToFind = 9; EDU>> FindNumberLocation(TeamAges,AgeToFind) ans = 4 EDU>> FindNumberLocation(TeamAges,77) NaN [ 4, 2, 9] 9 Let us write a function that takes in an array, a number to find in that array, and returns the location of that number in the array. If the number is not in the array, it return NaN FindNumberLocation 3

Finding Location of Given Number Important note: The previous slide did not specify what to do if the target number appears more than once. It is our job to clarify these things We could: Report the first occurrence Report the second occurrence Do something else [ 9, 2, 9] 9 Let us write a function that takes in an array, a number to find in that array, and returns the location of that number in the array. If the number is not in the array, it return NaN FindNumberLocation 3

NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location end EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 3

NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location end Does our code find the first or last occurrence of a number? It finds the last occurrence In the next slide we will see how to find the first instead EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 3

NumLocation = NaN; % Assume the number does not exist for now function NumLocation = FindNumberLocation(ArrayToSearch,TargetNum) NumLocation = NaN; % Assume the number does not exist for now for i = 1 : length(ArrayToSearch) if ArrayToSearch(i) == TargetNum NumLocation = i; % Assumption was wrong, record the location break; end The matlab command break terminates a for or while loop. The moment that Matlab encounters a break it jumps to the end of the loop and continues with the rest of the program… EDU>> TeamAges = [ 5 , 6, 5, 9 , 7]; EDU>> FindNumberLocation(TeamAges,5) ans = 1

Finding Vowel Count ‘eamonn’ 3 Let us write a function that takes in a string, and returns a count of how many vowels it has. Let us use our strategy of solving a simpler problem and working our way upto the final solution… ‘eamonn’ Let us use our strategy of echoing to the screen so we can debug the code.. FindVowels 3

function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if StringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end EDU>> FindVowels('eamonn') An a was found when i was 2 ans = 1 EDU>> FindVowels('abba') An a was found when i was 1 An a was found when i was 4 2

We are not finding uppercase ‘A’ function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if StringToSearch(i) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end There is a bug! We are not finding uppercase ‘A’ EDU>> FindVowels('ABBA') ans =

There is a simple fix function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if lower(StringToSearch(i)) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end There is a simple fix EDU>> FindVowels('ABBA') An a was found when i was 1 An a was found when i was 4 ans = 2

We can begin to add more code.. function VowelCount = FindVowels(StringToSearch) VowelCount = 0; % Assume there are no vowels for i = 1 : length(StringToSearch) if lower(StringToSearch(i)) == 'a' VowelCount = VowelCount + 1; disp(['An a was found when i was ', num2str(i)]) end if lower(StringToSearch(i)) == 'e' disp(['An e was found when i was ', num2str(i)]) EDU>> FindVowels('eamonn') An e was found when i was 1 An a was found when i was 2 ans = 2 We can begin to add more code..

Finding The Largest Value Let us write a function that finds the largest number in an array. As it happens, the built in max function does this, but we need to sharpen our skills… [ 1 3 4 5] EamonnsMax 5

1 4 2 8 function LargestNumber = EamonnsMax(InputArray) LargestNumber = -Inf; % Assume this is the largest number for i = 1 : length(InputArray) if InputArray(i) > LargestNumber disp('I need to update the largest number') LargestNumber = InputArray(i); end EDU>> EamonnsMax([1 4 2 8 4]) I need to update the largest number ans = 8 EDU>> EamonnsMax([4 3 2 1]) 4 1 4 2 8