1 / 36

Lecture 16

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

miriam-love
Download Presentation

Lecture 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 16 Chapters 10 and 11

  2. 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 10.3.4 Building a Collection 10.3.5 Mapping a Collection 10.3.6 Filtering a Collection 10.3.7 Summarizing a Collection 10.3.8 Searching a Collection 10.3.9 Sorting a Collection 10.4 Solving Larger Problems

  3. 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.

  4. 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.

  5. Consider Some Operations on Collections

  6. Cell Array and Struct AcellAray={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 the cell array is intended

  7. Insertion Operation in the Front,Cell Array >> item = 'stuff' item = stuff >> AcellArray = [{item} AcellArray] AcellArray = 'stuff' [3] [1x3 double] 'abcde' >>

  8. 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'

  9. 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(?)

  10. 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)]; end function rWe = areWeThereYet(item, cell) rWe = 1; end

  11. 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.

  12. Possible Types Possibilities 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

  13. Traversal* Code Example function 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)); end end end *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 to derive newCA.

  14. Result of Traversal makeEvenCellArray(testCA) newItem = 0 newItem = 0 0 0 ans = Columns 1 through 6 'other stuff' 'stuff' [0] [1x3 double] 'abcde' 'stuff' Column 7 'other stuff'

  15. Build Array outArray = 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

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

  17. 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'

  18. 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 preferred howMany = length(inStructArray); for structIndex = 1:howMany result(structIndex).person = inStructArray(structIndex).name; result(structIndex).UTCdate = inStructArray(structIndex).time; end

  19. 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 individual element is >3 With filtering, one can keep the elements that pass through the filter, or the elements that do not pass through the filter, or keep both, separated or all three.

  20. General Filtering Initialize result For items in collection Compare with criterion Insert the item into the collection corresponding Finalize result(s)

  21. 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

  22. 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.

  23. Search Using Recursion - 1 function result = recursivePresenceTest(needle, sortedHaystack) %true or false whether something was found %uses order in %uses power of 2 size -- otherwise problem wholeHaystack = 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)); end else if needle == sortedHaystack(1) result = 1; elseif needle == sortedHaystack(2) result = 1; else result = 0; end end end

  24. 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

  25. Sorting • Chapter 16

  26. 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?

  27. 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.

  28. 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.

  29. 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.

  30. 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

  31. Figures • Simplest: use the function plot • Next, manage figures with the function figure >> figureH= figure figureH = 1 >> anotherFigureH= figure anotherFigureH = 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.

  32. Using the figure function

  33. Using the plot function clf reset omega=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 off title('Impedance of Resistor vs. Frequency'); subplot(4,1,2) plot(omega, zLi, 'g') hold off title('Impedance of Inductor vs. Frequency'); • subplot(4,1,2) • plot(omega, zLi, 'g') • hold off • title('Impedance of Inductor vs. Frequency'); • subplot(4,1,3) • plot(omega, zCi, 'k') • hold off • title('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');

  34. Subplots

  35. 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

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

More Related