lecture 16
Skip this Video
Download Presentation
Lecture 16

Loading in 2 Seconds...

play fullscreen
1 / 36

Lecture 16 - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Lecture 16' - miriam-love

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 16

Lecture 16

Chapters 10 and 11

outline from chapter 10
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

problem solving
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.
simple problem definition
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.

cell array and struct
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

insertion operation in the front cell array
Insertion Operation in the Front,Cell Array

>> item = \'stuff\'

item =


>> AcellArray = [{item} AcellArray]

AcellArray =

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


insertion operation in the back cell array
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\'
insertion not necessarily at front or back
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(?)
na ve insertion
Naïve Insertion

function newCellArray = insertIntoCellArray (ca, item)

ins = 1;

while ins <= length (ca) && (~areWeThereYet(item, ca(ins)))

ins = ins + 1;


newCellArray = [ca(1:ins-1) {item} ca(ins:end)];


function rWe = areWeThereYet(item, cell)

rWe = 1;


traversal of a collection
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.
possible types
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

traversal code example
Traversal* Code Example

function newCA = makeEvenCellArray(cA)

howManyContainers = length(cA);


for thisContainer = 1:howManyContainers

thisItem = cA{thisContainer};

switch class(thisItem)

case \'char\'

sprintf(\'Container %d contains a char %s\',thisContainer, thisItem);


case \'double\'

newItem =zeros(size(thisItem))

sprintf(\'Container %d contains a double\', thisContainer);



sprintf(\'Didn\'\'t handle this case, which is %s\', thisContainer, class(thisItem));




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

result of traversal
Result of Traversal


newItem =


newItem =

0 0 0

ans =

Columns 1 through 6

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

Column 7

\'other stuff\'

build array
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)...



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


build cell array
Build Cell Array

>> A{1}=42

A =


>> B{1}={[4 6]}

B =

{1x1 cell}

>> C={3,[1,2,3], \'abcde\'}

C =

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

build struct
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\'

map array of struct
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;


filter vector
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.

general filtering
General Filtering

Initialize result

For items in collection

Compare with criterion

Insert the item into the

collection corresponding

Finalize result(s)

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


  • 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.
search using recursion 1
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));


result = recursivePresenceTest(needle, sortedHaystack(halfHaystack+1:end));


else if needle == sortedHaystack(1)

result = 1;

elseif needle == sortedHaystack(2)

result = 1;


result = 0;




searching using recursion 2
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 =


  • Chapter 16
combining operations to solve a problem example problem
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?
combining operations to solve a problem example data
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.
break the problem down into factors
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.
combine in a c ontrolled fashion
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.
outline from chapter 11
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
  • Simplest: use the function plot
  • Next, manage figures with the function figure

>> figureH= figure

figureH =


>> anotherFigureH= figure

anotherFigureH =


    • 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.
using the plot function
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);



plot(omega, zRr,\'r\')

hold off

title(\'Impedance of Resistor vs. Frequency\');


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\');
enhancing the plot
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
  • Within one figure, multiple plots
  • Not the same as hold on, where the axes are shared