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

28
Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing

Transcript of Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

Page 1: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

Computer Science 121

Scientific ComputingWinter 2012

Chapter 4Collections and Indexing

Page 2: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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)

Page 3: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.1 Indexing● Consider census data for a single street:

>> elmstreet = [3 5 2 0 4 5 1];● Matlab can give us various stats about this data

>> sum(elmstreet) % total # residents

ans = 20

>> mean(elmstreet) % mean household size

ans = 2.8571

>> max(elmstreet) % largest household size

ans = 5

>> min(elmstreet) % smallest household size

ans = 0

Page 4: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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

Page 5: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.1 Indexing: find● Recall boolean operators on vectors

>> elmstreet == 0

ans = 0 0 0 1 0 0 0

● The find operator tells us the indices of the non-zero elements

>> find(elmstreet == 0)

ans = 4

>> find(elmstreet > 2)

ans = 1 2 5 6

>> find(elmstreet < 0)

ans = []

Page 6: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.1 Indexing: First and last Elements

● First element has index 1 (unlike Java, C++)>> elmstreet

ans = 3 5 2 0 4 5 1

>> elmstreet(1)

ans = 3

● Last element can be referenced by special end index

>> elmstreet(end)

ans = 1

Page 7: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.1 Indexing: Subsequences

● Can use a vector of indices instead of a single index

>> elmstreet([1 3 5])

ans = 3 2 4

>> elmstreet([1 3 5]) = -1

elmstreet = -1 5 -1 0 -1 5 1

Page 8: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.1 Indexing: Extending a Vector

● Use end+1 to add an element at end of vector:>> elmstreet

ans = 3 5 2 0 4 5 1

>> elmstreet(end+1) = 8

elmstreet = 3 5 2 0 4 5 1 8

● If we go beyond end, Matlab fills gaps with 0's:>> elmstreet(12) = 9

elmstreet = 3 5 2 0 4 5 1 8 0 0 0 9

Page 9: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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 = 0 1 1

>> fib(end+1) = fib(end) + fib(end-1)

fib = 0 1 1 2

>> fib(end+1) = fib(end) + fib(end-1)

fib = 0 1 1 2 3

etc.

Page 10: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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

Page 11: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.2 Matrices● We can store such data in a matrix:

>> elmstreet = [3 2 1 35000; 5 2 3 41000;

2 1 1 25000; 2 2 0 56000; 4 2 2 62000; 5 3 2 83000; 1 1 0 52000]

● Household index is implicit (as row number)

Page 12: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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 = 25000>> elmstreet(4, 3)ans = 0

Page 13: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.2 Matrices● As with vectors, can access part of matrix by using

a vector of indices

>> elmstreet([4 5 7], 4)ans = 56000 62000 52000

● Grab a whole row using colon notation

>> elmstreet(1, :) % whole first rowans = 3 2 1 35000

Page 14: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.2 Matrices● Also works for columns:

>> elmstreet(:, 1) % whole first colans = 3

522451

Page 15: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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

Page 16: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

● As with a vector, we can do operations on a scalar and a matrix:

>> [1 2 3; 4 5 6; 7 8 9] * 2ans = 2 4 6 8 10 12

14 16 18

Page 17: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

● ... 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 .* bans = 1 0 3 0 0 6 7 8 0

Page 18: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

● Of course, matrices must be same size for .*

>> [1 2 3; 4 5 6; 7 8 9] .* [3 4; 5 6]??? Error using ==> timesMatrix dimensions must agree...And your socks don’t match either.

Page 19: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

● We can get a lot of mileage by combining colon and other operations

>> children = elmstreet( : , 3)children = 1 3 1 0 2 2 0>> nokidshouses = find(children == 0)nokidshouses = 4 7>> incomenokids = ... elmstreet(nokidshouses, 4)incomenokids = 56000

52000>> mean (incomenokids)ans = 55000

Page 20: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

● Some matrix operations yield a vector:

>> [r,c] = ... find (elmstreet >3 & elmstreet <= 5)

r = 2

56

c = 111

Page 21: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.3 Mixed Data Types

● Not all data is (are?) numerical:

Page 22: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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!

Page 23: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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 = 3pt =

x : 3

>> pt.name = ‘R.E. Lee'pt =

x: 3name: R.E. Lee

Page 24: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.3 Mixed Data Types: Structures

● Structure arrays contain structures with similar contents:

>> people(3).name = 'Stimpy';>> people(3).IQ = 80people = 1x3 struct array with fields: name IQ

Page 25: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.3 Mixed Data Types: Structures

● Matlab fills in missing array members with empty structures:

>> people(1) ans = name: [] IQ: []

Page 26: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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]

Page 27: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

4.3 Mixed Data Types: Cell Arrays

● Cell array is referenced using curly braces { , }

>> people{1, :}ans =Ren

ans = 60

Page 28: Computer Science 121 Scientific Computing Winter 2012 Chapter 4 Collections and Indexing.

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]