Top Banner
Lecture 16 Chapters 10 and 11
36

Lecture 16

Jan 04, 2016

Download

Documents

miriam-love

Lecture 16. Chapters 10 and 11. Outline from Chapter 10. 10.1 Solving Simple Problems 10.2 Assembling Solution Steps for Processing Collections 10.3 Summary of Operations on Collections 10.3.1 Basic Operations 10.3.2 Inserting into a Collection 10.3.3 Traversing a Collection - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture  16

Lecture 16

Chapters 10 and 11

Page 2: Lecture  16

Outline from Chapter 10

10.1 Solving Simple Problems10.2 Assembling Solution Steps for Processing Collections10.3 Summary of Operations on Collections

10.3.1 Basic Operations10.3.2 Inserting into a Collection10.3.3 Traversing a Collection10.3.4 Building a Collection10.3.5 Mapping a Collection10.3.6 Filtering a Collection10.3.7 Summarizing a Collection10.3.8 Searching a Collection10.3.9 Sorting a Collection

10.4 Solving Larger Problems

Page 3: Lecture  16

Problem Solving

• Build from the data you know– What data do you have, not necessarily the

representation it has• Aim for what you want– Can you divide your goal into subgoals?– Similar but easier goals?

• Consider the tools you have available– How could any of the tools you have help you

transform the data you have to achieve any of the subgoals you have.

Page 4: Lecture  16

Simple Problem (Definition*)

• Define the input data• Define the output data• Discover the underlying equations to solve the

problem• Implement (code)• Verify – Analysis, Test are methods of Verification, not

necessarily complete

*That is, if the method given can be done, the problem is “simple”. Not an adequate definition.

Page 5: Lecture  16

Consider Some Operations on Collections Operation Comment

Insert Obtain an updated collection with an additional element

Traverse (non-modifying)

Examine each element in a collection (at some specified depth in data structure)

Build Create a collection

Traverse (modifying) also known as Map

Each element of the collection has the opportunity to be transformed in some way

Filter Obtain some part of the collection, according to some rule

Extract a Statistic Obtain a value that is a function of the contents of the collection

Search Seek an element possibly in the collection, obtain it, or its location, if it is present

Sort Obtain a collection, organized according to some principle.

Page 6: Lecture  16

Cell Array and StructAcellAray={3,[1,2,3] 'abcde'}

AcellArray =

[3] [1x3 double] 'abcde'

>> Astruct.scalar = 3

Astruct =

scalar: 3

>> Astruct.numList = [1,2,3]

Astruct =

scalar: 3 numList: [1 2 3]

>> Astruct.theString = 'abcde'

Astruct =

scalar: 3 numList: [1 2 3] theString: 'abcde'

A structure looks like a cell array, where field names have been added, as names on the containers,to ease identification of which element in thecell array is intended

Page 7: Lecture  16

Insertion Operation in the Front,Cell Array

>> item = 'stuff'

item =

stuff

>> AcellArray = [{item} AcellArray]

AcellArray =

'stuff' [3] [1x3 double] 'abcde'

>>

Page 8: Lecture  16

Insertion Operation in the Back,Cell Array

>> item2 = 'other stuff'

item2 =

other stuff

>> AcellArray = [AcellArray {item}]

AcellArray =

'stuff' [3] [1x3 double] 'abcde' 'stuff‘

>> AcellArray = [AcellArray {item2}]

AcellArray =

Columns 1 through 5

'stuff' [3] [1x3 double] 'abcde' 'stuff'

Column 6

'other stuff'

Page 9: Lecture  16

Insertion, Not Necessarily at Front or Back

• We could access the cell array from the beginning, look at each container, until we arrive at the insertion point, separate the cell array at the insertion point, and create a cell array with the new item sandwiched in.

• This approach performs as O(n).• If we used recursion to find the insertion

point, would it be faster? O(?)

Page 10: Lecture  16

Naïve Insertion

function newCellArray = insertIntoCellArray (ca, item) ins = 1;while ins <= length (ca) && (~areWeThereYet(item, ca(ins))) ins = ins + 1;end;newCellArray = [ca(1:ins-1) {item} ca(ins:end)];endfunction rWe = areWeThereYet(item, cell)rWe = 1;end

Page 11: Lecture  16

Traversal of a Collection

• Every top level element of the collection is processed.

• Could switch on type of contents of a container, use cases for possible types.

Page 12: Lecture  16

Possible TypesPossibilities are: double -- Double precision floating point number array (this is the traditional MATLAB matrix or array) single -- Single precision floating point number array logical -- Logical array char -- Character array cell -- Cell array struct -- Structure array function_handle -- Function Handle int8 -- 8-bit signed integer array uint8 -- 8-bit unsigned integer array int16 -- 16-bit signed integer array uint16 -- 16-bit unsigned integer array int32 -- 32-bit signed integer array uint32 -- 32-bit unsigned integer array int64 -- 64-bit signed integer array uint64 -- 64-bit unsigned integer array <class_name> -- MATLAB class name for MATLAB objects <java_class> -- Java class name for java objects

Page 13: Lecture  16

Traversal* Code Examplefunction newCA = makeEvenCellArray(cA)howManyContainers = length(cA);newCA{howManyContainers}=0;for thisContainer = 1:howManyContainers thisItem = cA{thisContainer}; switch class(thisItem) case 'char' sprintf('Container %d contains a char %s',thisContainer, thisItem); newCA{thisContainer}=thisItem; case 'double' newItem =zeros(size(thisItem)) sprintf('Container %d contains a double', thisContainer); newCA{thisContainer}=newItem; otherwise sprintf('Didn''t handle this case, which is %s', thisContainer, class(thisItem)); endendend

*This code traverses cA without modifying it. (Call by value)This code builds newCA.This code maps cA to newCA, because it uses a transformation upon cA toderive newCA.

Page 14: Lecture  16

Result of TraversalmakeEvenCellArray(testCA)

newItem =

0

newItem =

0 0 0

ans =

Columns 1 through 6

'other stuff' 'stuff' [0] [1x3 double] 'abcde' 'stuff'

Column 7

'other stuff'

Page 15: Lecture  16

Build ArrayoutArray = zeros(allRanges);

for rowIndex = 1:nRows if dependentVariables(rowIndex) == 1 %if there is a b to be added on for independentVariableIndex = 1:nIndependentVariables columnValue(independentVariableIndex) = ... nums(rowIndex, independentVariableIndex)... -allMins(independentVariableIndex)+1; end outArray(columnValue(1), columnValue(2))=

outArray(columnValue(1), columnValue(2))+1;end

Page 16: Lecture  16

Build Cell Array

>> A{1}=42A = [42]>> B{1}={[4 6]}B = {1x1 cell}>> C={3,[1,2,3], 'abcde'}C = [3] [1x3 double] 'abcde'

Page 17: Lecture  16

Build Struct• The structure is created incrementally—it

emerges:>> myStruct.item1 = 3

myStruct =

item1: 3

>> myStruct.item2 = 'abce'

myStruct =

item1: 3 item2: 'abce'

myStruct.goodFieldName = 'example'

myStruct =

item1: 3 item2: 'abce' goodFieldName: 'example‘

>> myOtherStruct = rmfield(myStruct, 'item2')

myOtherStruct =

item1: 3 goodFieldName: 'example'

Page 18: Lecture  16

Map Array of Struct

function result = mapStructArray(inStructArray)%convert one kind of structure into another type of structure%can be done with XML, XSLT, which is preferredhowMany = length(inStructArray);for structIndex = 1:howMany result(structIndex).person = inStructArray(structIndex).name; result(structIndex).UTCdate = inStructArray(structIndex).time; end

Page 19: Lecture  16

Filter Vector>> aVect = [ 1 2 3 4 5]

aVect =

1 2 3 4 5

>> bVect = aVect >3

bVect =

0 0 0 1 1

>> cVect = find (aVect>3)

cVect =

4 5

cVect is the result of a filtering operation on aVect, according to whether the individualelement is >3

With filtering, one can keep the elements thatpass through the filter, or the elements thatdo not pass through the filter,or keep both, separatedor all three.

Page 20: Lecture  16

General Filtering

Initialize result

For items in collection

Compare with criterion

Insert the item into thecollection corresponding

Finalize result(s)

Page 21: Lecture  16

Summarizing• Traverse the collection, and determine some summary from

it.• In statistics, there is the notion of the “sufficient statistic”*:

– "when no other statistic which can be calculated from the same sample provides any additional information as to the value of the parameter“

– If we had an equation for a sufficient statistic appropriate to our circumstances, we would use that equation, traversing our (sample) data, and calculate it.

– For example, if we know our data is distributed as a normal distribution, the mean plus the variance fully characterize the distribution, and we can calculate the mean and variance of data.

http://en.wikipedia.org/wiki/Sufficient_statistic

Page 22: Lecture  16

Searching

• The book says searching is traversing, where traversing is visiting all elements, at least all top level elements.

• Some would disagree: What if you know there is at most one, and you find it. Must you visit all the rest?

• What if the data, like a dictionary, is ordered? Once you get past the point where the sought item would be, even if it is not found, you can stop.

Page 23: Lecture  16

Search Using Recursion - 1function result = recursivePresenceTest(needle, sortedHaystack)%true or false whether something was found%uses order in %uses power of 2 size -- otherwise problemwholeHaystack = length(sortedHaystack);halfHaystack = wholeHaystack/2;sprintf('Haystack size is %d', wholeHaystack)if halfHaystack >=2 if sortedHaystack(halfHaystack)> needle result = recursivePresenceTest(needle, sortedHaystack(1:halfHaystack)); else result = recursivePresenceTest(needle, sortedHaystack(halfHaystack+1:end)); endelse if needle == sortedHaystack(1) result = 1; elseif needle == sortedHaystack(2) result = 1; else result = 0; end endend

Page 24: Lecture  16

Searching Using Recursion-2>> sortedHaystack = [ 1 3 5 7 9 11 13 15]

sortedHaystack =

1 3 5 7 9 11 13 15>> recursivePresenceTest(5, sortedHaystack)

ans =

Haystack size is 8

ans =

Haystack size is 4

ans =

Haystack size is 2

ans =

1

Page 25: Lecture  16

Sorting

• Chapter 16

Page 26: Lecture  16

Combining Operations to Solve a Problem, Example Problem

• Going fishing while on vacation:where should we go to have the best chance of a catch that we like?

• How likely are we to catch something we like?

Page 27: Lecture  16

Combining Operations to Solve a Problem, Example Data

• http://www.niwa.co.nz/news-and-publications/publications/all/wa/15-1/models

• Finding fish with statistical models• Over the years, a considerable number of New Zealand’s rivers

and streams have been fished to collect information about the distribution and abundance of native fish such as eels and galaxids, and introduced fish such as brown and rainbow trout. This information is stored in NIWA’s Freshwater Fish Database, which now contains data from over 20 000 sites. We have been working with these data to create statistical models that describe how the likelihood of capturing individual fish species varies depending on the nature of the river or stream.

Page 28: Lecture  16

Break the Problem Down into Factors

• We can use each species model to predict the probability of capture for rivers and streams throughout New Zealand, including those not fished. Predictions are made on the basis of the statistical description of the relationship between capture of each species and the environment, adjusted for the time of year and fishing method. The predictions reflect the individual species’ responses to factors such as the distance from the coast, downstream slopes, the temperature, the amount of water flow and its variability, the degree of riparian shading, the frequency of rain-days, and the degree of native vegetation cover in the upstream catchment.

• Can use fields of a struct or other heterogeneous collection to classify data, for example, make relatively fine distinctions as to location, species, time of year, weather.

• Data stored include the site location, the species present, their abundance and size, as well as information such as the fishing method used and a physical description of the site. The latter includes an assessment of the habitat type, substrate type, available fish cover, catchment vegetation, riparian vegetation, water widths and depths, and some water quality measures.

Page 29: Lecture  16

Combine in a Controlled Fashion

• By combining the predictions for all these species, we can create maps showing the expected native fish community patterns for our rivers and streams. This is valuable for conservation management, allowing assessment of the degree of protection afforded to different fish species and communities.

• Can filter for desired species, location.

Page 30: Lecture  16

Outline from Chapter 11

• 11.1 Plotting in General• 11.2 2-D Plotting• 11.3 3-D Plotting• 11.4 Surface Plots• 11.5 Interacting with Plotted Data

Page 31: Lecture  16

Figures

• Simplest: use the function plot• Next, manage figures with the function figure

>> figureH = figurefigureH = 1>> anotherFigureH = figureanotherFigureH = 2– FIGURE(H) makes H the current figure, forces it to become visible,– and raises it above all other figures on the screen. If Figure H– does not exist, and H is an integer, a new figure is created with– handle H.

Page 32: Lecture  16

Using the figure function

Page 33: Lecture  16

Using the plot functionclf resetomega=linspace(pi/128,pi/64, 600);L = 0.1; C = 1; R = 3;howMany = length(omega);zLr = ones(howMany); zLi = ones(howMany);zCr = ones(howMany); zCi = ones(howMany);zRr = ones(howMany)*R; zRi =

zeros(howMany);for omegaIndex = 1:howMany zLr(omegaIndex) = 0; zLi(omegaIndex) = omega(omegaIndex)*L; zCr(omegaIndex)= 0; zCi(omegaIndex) = (-1)/(omegaIndex*C);end;subplot(4,1,1)plot(omega, zRr,'r')hold offtitle('Impedance of Resistor vs. Frequency');subplot(4,1,2)plot(omega, zLi, 'g')hold offtitle('Impedance of Inductor vs. Frequency');

subplot(4,1,2)plot(omega, zLi, 'g')hold offtitle('Impedance of Inductor vs. Frequency');subplot(4,1,3)plot(omega, zCi, 'k')hold offtitle('Impedance of Capacitor vs. Frequency');scalarDenom = (zLr + zCr) .* (zLr + zCr) + (zLi +

zCi) .* (zLi + zCi);numR = (zLr + zCr) .* (zLr .* zCr) - ((zLi + zCi) .*

(zLi .* zCi));parallelZAllr = (numR ./ scalarDenom) + zRr;parallelZLpCr = (numR ./ scalarDenom);subplot(4,1,4)plot(omega, parallelZLpCr ./ parallelZAllr, 'r')title('Impedance of (Capacitor || Inductor)/(R+C||

L) vs. Frequency');

Page 34: Lecture  16

Subplots

Page 35: Lecture  16

Enhancing the Plot

• AXIS Control axis scaling and appearance on the current plot.

• COLORMAP(MAP) sets the current figure's colormap to MAP.– This is a sequence of colors. – 15 colormaps are given in textbook page A-7

• Grid, legend, title, text, x-, y-, and z-labels• Hold on – for multiple plots sharing the same set of axes• View—the angles (degrees) from which to view a plot• Shading—method for shading surfaces

Page 36: Lecture  16

Subplots

• Within one figure, multiple plots• Not the same as hold on, where the axes are

shared