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

Slides:



Advertisements
Similar presentations
Question Score Question 2 Score1 Question 3 Score2.
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Built-in Data Structures in Python An Introduction.
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Integers. Magic Squares
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Pointers and Dynamic Arrays
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
REPETITION CONTROL STRUCTURE
Manipulating MATLAB Matrices Chapter 4
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Working with Collections of Data
CS005 Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming:
CS005 Introduction to Programming:
Dictionaries, File operations
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
CS005 Introduction to Programming
Introduction to Linked Lists
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Chapter 9 Arrays.
Variables ICS2O.
Flooding © 2018.
CS005 Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Vectors and Matrices I.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Data Structures – 1D Lists
Bryan Burlingame 28 November 2018
ARRAYS 1 GCSE COMPUTER SCIENCE.
CS005 Introduction to Programming: Matlab
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Logical Operations In Matlab.
SQL: Structured Query Language
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Arrays and Matrices in MATLAB
Array Creation ENGR 1181 MATLAB 02.
Programming Control Structures with JavaScript Part 2
MATLAB Programming Basics Copyright © Software Carpentry 2011
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Loops and Arrays in JavaScript
JavaScript: Arrays.
CHAPTER 4: Lists, Tuples and Dictionaries
Data Structures & Algorithms
Chapter 3: Selection Structures: Making Decisions
Move this box to see the hints (there are two hints for this code)
Matrices in MATLAB Dr. Risanuri Hidayat.
EECS Introduction to Computing for the Physical Sciences
C++ Array 1.
Scaffolding a Math Problem: Solving for a Single Variable
Week 7: Computer Tools for Problem Solving and Critical Thinking
Class code for pythonroom.com cchsp2cs
Dictionary.
Week 7 - Monday CS 121.
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

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’)