Computer Science 121 Scientific Computing Winter 2014 Chapter 4 Collections and Indexing.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Advertisements

Chapter 8. Data Structure: A variable that stores more than one value Matrices/vectors and character arrays are types of data structures MATLAB also provides.
Lecture 4.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Maths for Computer Graphics
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
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.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Chapter 8 Arrays and Strings
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
Matlab tutorial course Lesson 2: Arrays and data types
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.
Lists in Python.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Multiplying Matrices Algebra 2—Section 3.6. Recall: Scalar Multiplication - each element in a matrix is multiplied by a constant. Multiplying one matrix.
Chapter 1 Section 1.5 Matrix Operations. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or an.
Arrays.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Computer Science 121 Scientific Computing Winter 2016 Chapter 4 Collections and Indexing.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Project Teams Project on Diffusion will be a TEAM project. 34 Students in class so 6 teams of 5 and one Team of 4. Determine members of team and a team.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
13.4 Product of Two Matrices
Multiplying Matrices.
Other Kinds of Arrays Chapter 11
Multiplying Matrices Algebra 2—Section 3.6.
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Warm Up Use scalar multiplication to evaluate the following:
Multiplying Matrices.
Matlab tutorial course
Vectors and Matrices I.
CSCI N207 Data Analysis Using Spreadsheet
Unit 3: Matrices
Multiplying Matrices.
EECS Introduction to Computing for the Physical Sciences
Multiplying Matrices.
Working with Arrays in MATLAB
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

Computer Science 121 Scientific Computing Winter 2014 Chapter 4 Collections and Indexing

● We've seen two kinds of collection –Vector (sequence of numbers) –Text/string (sequence of characters) ● Two main issues –How to access individual elements of a collection –How to group related elements together (even when their types differ)

4.1 Indexing ● Consider census data for a single street: >> elmstreet = [ ]; ● Matlab can give us various stats about this data >> sum(elmstreet) % total # residents ans = 20 >> mean(elmstreet) % mean household size ans = >> max(elmstreet) % largest household size ans = 5 >> min(elmstreet) % smallest household size ans = 0

4.1 Indexing ● Some data may be bogus >> min(elmstreet) % smallest size ans = 0 ● Need to know bogus values, and where they “live” ● In general, need to know –Value of an element –Position (index) of the element

4.1 Indexing: find ● Recall boolean operators on vectors >> elmstreet == 0 ans = ● The find operator tells us the indices of the non-zero elements >> find(elmstreet == 0) ans = 4 >> find(elmstreet > 2) ans = >> find(elmstreet < 0) ans = []

4.1 Indexing: First and last Elements ● First element has index 1 (unlike Java, C++) >> elmstreet ans = >> elmstreet(1) ans = 3 ● Last element can be referenced by special end index >> elmstreet(end) ans = 1

4.1 Indexing: Subsequences ● Can use a vector of indices instead of a single index >> elmstreet([1 3 5]) ans = >> elmstreet([1 3 5]) = -1 elmstreet =

4.1 Indexing: Extending a Vector ● Use end+1 to add an element at end of vector: >> elmstreet ans = >> elmstreet(end+1) = 8 elmstreet = ● If we go beyond end, Matlab fills gaps with 0's: >> elmstreet(12) = 9 elmstreet =

Fibonacci Redux ● With vectors, we only need a single variable, line (versus three) to do Fibonacci: >> fib = [0 1]; >> fib(end+1) = fib(end) + fib(end-1) fib = >> fib(end+1) = fib(end) + fib(end-1) fib = >> fib(end+1) = fib(end) + fib(end-1) fib = etc.

4.2 Matrices ● Lots of data are best represented as tables:

4.2 Matrices ● We can store such data in a matrix: >> elmstreet = [ ; ; ; ; ; ; ] ● Household index is implicit (as row number)

4.2 Matrices ● Like length operator for vectors, size operator reports size of matrix: >> size(elmstreet) ans = 7 4 ● With matrices, we use two indices (instead of one) for referencing values: >> elmstreet(3, 4) ans = >> elmstreet(4, 3) ans = 0

4.2 Matrices ● As with vectors, can access part of matrix by using a vector of indices >> elmstreet([4 5 7], 4) ans = ● Grab a whole row using colon notation >> elmstreet(1, :) % whole first row ans =

4.2 Matrices ● Also works for columns: >> elmstreet(:, 1) % whole first col ans =

4.2 Matrices ● Recall that a scalar is a length-one vector >> length(7) ans = 1 ● A scalar is also a one-by-one matrix >> size(7) ans = 1 1

● As with a vector, we can do operations on a scalar and a matrix: >> [1 2 3; 4 5 6; 7 8 9] * 2 ans =

●... and element-by-element on two matrices: >> a = [1 2 3; 4 5 6; 7 8 9]; >> b = [1 0 1; 0 0 1; 1 1 0]; >> a.* b ans =

● Of course, matrices must be same size for.* >> [1 2 3; 4 5 6; 7 8 9].* [3 4; 5 6] ??? Error using ==> times Matrix dimensions must agree... And your socks don’t match either.

● We can get a lot of mileage by combining colon and other operations >> children = elmstreet( :, 3) children = >> nokidshouses = find(children == 0) nokidshouses = 4 7 >> incomenokids =... elmstreet(nokidshouses, 4) incomenokids = >> mean (incomenokids) ans = 55000

● Some matrix operations yield a vector: >> [r,c] =... find (elmstreet >3 & elmstreet <= 5) r = c = 1

4.3 Mixed Data Types ● Not all data is (are?) numerical:

4.3 Mixed Data Types ● We can't put text into a matrix >> smiths(1,1) = 'Emily' ??? Subscripted assignment dimensions mismatch… oh no you di’n’t! ● Because how do we know that next element ( 'George' ) will be same size? ● Old-school solution was to enforce fixed sizes for everything – led to Y2K problem!

4.3 Mixed Data Types: Structures ● Structures (a.k.a. Data Structures) allow us to put different types of data into the same collection: >> pt.x = 3 pt = x : 3 >> pt.name = ‘R.E. Lee' pt = x: 3 name: R.E. Lee

4.3 Mixed Data Types: Structures ● Structure arrays contain structures with similar contents: >> people(3).name = 'Stimpy'; >> people(3).IQ = 80 people = 1x3 struct array with fields: name IQ

4.3 Mixed Data Types: Structures ● Matlab fills in missing array members with empty structures: >> people(1) ans = name: [] IQ: []

4.3 Mixed Data Types: Cell Arrays ● A cell array is a matrix that can contain any type of data: >> people = {'Ren', 60;... 'Stimpy', 80;... 'Muddy', 100} people = ‘Ren' [ 60] 'Stimpy’ [ 80] 'Muddy' [100]

4.3 Mixed Data Types: Cell Arrays ● Cell array is referenced using curly braces {, } >> people{1, :} ans = Ren ans = 60

4.3 Mixed Data Types: Cell Arrays ● But if we want to store output values, we use ordinary parens: >> hs = people{1, :} ??? Illegal right hand side in assignment. Too many elements. Please make it stop... >> hs = people(1,:) hs = 'Ren' [60]