Lecture 18 Oct 24 Cell arrays (Ch 4), structures recursion (Ch 12)

Slides:



Advertisements
Similar presentations
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Chapter 9: Searching, Sorting, and Algorithm Analysis
HST 952 Computing for Biomedical Scientists Lecture 9.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Lecture 2 Aug goals: Introduction to recursion examples of recursive programs.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Programming with Collections Collections in Java Using Arrays Week 9.
Lecture 12 Oct 15 Recursion – more examples Chapter 8 problems and solutions Cell arrays, Chapter 10.
Chapter 8 Loops while loop syntax while ;... end;.
Lec 13 Oct 17 cell arrays structures advanced recursive programs.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Lec 13 Oct 20 cell arrays (Chapter 4) structures (Chapter 4) generating subsets generating permutations (HW 4 problem)
Chapter 8 Loops while loop syntax while ;... end;.
Lec 15 Oct 27 more examples of recursive programs more about cell arrays structures in Matlab.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Chapter 8 Loops while loop syntax while ;... end;.
Lec 14 Oct 22 more examples of recursive programs more about cell arrays structures in Matlab.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Lists in Python.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lecture 15 Chapters 6 and 7. Outline from Chapter Character String Concepts: Mapping and Casting 6.2 MATLAB Implementation Slicing and Concatenating.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Covenant College December 18, Laura Broussard, Ph.D. Visiting Professor COS 131: Computing for Engineers Chapter 7: Cell Arrays and Structures.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Python Programing: An Introduction to Computer Science
Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LINKED LISTS.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Java Review: Reference Types
Chapter 8 Arrays Objectives
Fondamenti di informatica structures
Chapter 8 Arrays Objectives
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Presentation transcript:

Lecture 18 Oct 24 Cell arrays (Ch 4), structures recursion (Ch 12)

Cell arrays suppose we want to represent a collection of sets such as: {1, 2, 4}, {3, 4, 6}, {7, 8} Each set can be represented by vector: [1, 2, 4], [3, 4, 6], [7, 8] >> A = [[ 1, 2, 4], [3, 4, 6], [7, 8]] A becomes [1, 2, 4, 3, 4, 6, 7, 8]

Cell arrays

Cell array – examples

Cell – operations

More about cell arrays and struct arrays Cell Arrays –Creating Cell Arrays –Accessing Cell Arrays –Using Cell Arrays –Processing Cell Arrays MATLAB Structures –Constructing and Accessing One Structure –Constructor Functions Structure Arrays –Constructing Structure Arrays –Accessing Structure Elements –Manipulating Structures

Concept: Collecting Dissimilar Objects Heterogeneous collections permit objects of different data types to be grouped in a collection. –They allow data abstraction to apply to a much broader range of content. –However, the fact that the contents of these collections may be of any data type severely restricts the operations that can be performed on the collections as a whole. –Whereas a significant number of arithmetic and logical operations can be performed on whole number arrays, algorithms that process heterogeneous collections must deal with the data contents one item at a time.

Cell Arrays Cell arrays, as the name suggests, have the general form of arrays and can be indexed numerically as arrays. However, each element of a cell array should be considered as a container in which one data object of any class can be stored. They can be treated as arrays of containers for the purpose of concatenation and slicing. However, if you wish to access or modify the contents of the containers, the cells must be accessed individually.

Creating Cell Arrays By assigning values individually to a variable indexed with braces: >> A{1} = 42 A = [42] By assigning containers individually to a variable indexed with brackets: >> B[1] = {[4 6]}; B = [1x2 double] By concatenating cell contents using braces {...}: >> C = {3, [1,2,3], 'abcde'} C = [3] [1x3 double] 'abcde' By concatenating cell containers: >> D = [A B C {'xyz'}] D = [42] [1x2 double] [3] [1x3 double] 'abcde' 'xyz'

Accessing Cell Arrays Continuing the previous examples, we have the following: >> E = D(2) % parentheses - a container E = [4 6] However, braces are used to access the contents of the containers as follows: >> D{2} % braces - the contents ans = 4 6 If the right-hand side of an assignment statement results in multiple cell arrays, the assignment must be to the same number of variables.

Using Cell Arrays Containing lists of possible values for switch/case statements Substituting for parameter lists in function calls –For example, suppose you have a function largest(a, b, c) that consumes three variables and produces the largest of the three values provided. It can be used in the following styles: A = 4; B = 6; C = 5; N = largest(A, B, C) params = { 4, 6, 5 }; N = largest(params{1:3})

Processing Cell Arrays Checking the class of the element can be achieved in one of two ways: –The function class(item) returns a string specifying the item type that can be used in a switch statement. –Individual test functions can be used in an if... elseif construct; –examples of the individual test functions are isa(item, 'class'), –iscell(...), ischar(...), islogical(...), isnumeric(...), and –isstruct(...)

MATLAB Structures Structures allow items in the collection to be indexed by field name. The data contained in a structure is referenced by field name, e.g., item1. The rules for making a field name are the same as those for a variable. Fields of a structure, like the elements of a cell array, are heterogeneous—they can contain any MATLAB object.

Constructing and Accessing a Structure To set the value of items in a structure A, the syntax is as follows: >> A.item1 = 'abcde' A = item1: 'abcde' >> A.item2 = 42 A = item1: 'abcde' item2: 42 Fields in a structure are accessed in the same way—by using the dotted notation. >> A.item2 = A.item2./ 2 A = item1: 'abcde' item2: 21

Manipulating Field Names To determine the names of the fields in a structure, the built-in function fieldnames(...) returns a cell array containing the field names as strings. >> names = fieldnames(A) names = 'item1' 'item2’ Fields can also be accessed “indirectly” by setting a variable to the name of the field, and then using parentheses to indicate that the variable contents should be used as the field name: >> fn = names{1}; >> A.(fn) = [A.(fn) 'fg'] A = item1: 'abcdefg' item2: 21

More about Field Names You can remove a field from a structure using the built-in function rmfield(...). Be careful. rmfield(...) returns a new structure with the requested field removed. It does not remove that field from your original structure. If you want the field removed from the original, you must assign the result from rmfield(...) to replace the original structure: >> A = rmfield(A, 'item1') A = item2: 21

Why Constructor Functions? Use constructor functions, as opposed to “manually” entering data into structures, for the following reasons: –Manual entry can result in strange behavior due to typographical errors or having fields in the wrong order –The resulting code is generally more compact and easier to understand –When constructing collections of structures, it enforces consistency across the collections

Built-in Constructor Function struct(…) >> struct('first','Fred',... 'last','Jones',... 'phone','(123) ',... 'birth', struct( 'day', 31,... 'month', 'February',... 'year', 1965 )) ans = first: 'Fred' last: 'Jones' phone: '(123) ' birth: [1x1 struct]

Custom Constructor Functions A typical custom constructor function function ans = makeCD(gn, ar, ti, yr, st, pr) % integrate CD data into a structure ans.genre = gn ; ans.artist = ar ; ans.title = ti; ans.year = yr; ans.stars = st; ans.price = pr; Usage: >> CD = makeCD('Blues', 'Charles, Ray’, 'Genius Loves Company', 2004, 4.5, ) CD = genre: 'Blues' artist: 'Charles, Ray' title: 'Genius Loves Company' year: 2004 stars: price:

Building Structure Arrays Manually >> entry(1).first = 'Fred'; >> entry(1).last = 'Jones'; >> entry(1).age = 37; >> entry(1).phone = ' (123) '; >> entry(2).first = 'Sally’; >> entry(2).last = 'Smith’; >> entry(2).age = 29; >> entry(2).phone = '(000) ' entry = 1x2 structure array with fields: first last age phone

Building Structure Arrays with struct(…) genres = {'Blues', 'Classical', 'Country' }; artists = {'Clapton, Eric', 'Bocelli, Andrea', … 'Twain, Shania' }; years = { 2004, 2004, 2004 }; stars = { 2, 4.6, 3.9 }; prices = { 18.95, 14.89, }; cds = struct( ‘genre’, genres, … 'artist', artists, … 'year', years, … 'stars', stars, … 'price', prices);

Building Structure Arrays with makeCD(…) cds(1) = makeCD('Blues', 'Clapton, Eric',... 'Sessions For Robert J', 2004, 2, ) cds(2) = makeCD('Classical',... 'Bocelli, Andrea', 'Andrea', 2004, 4.6, ) cds(3) = makeCD( 'Country', 'Twain, Shania',... 'Greatest Hits', 2004, 3.9, ) cds(4) = makeCD( 'Latin', 'Trevi, Gloria',... 'Como Nace El Universo', 2004, 5, ) cds(5) = makeCD( 'Rock/Pop', 'Ludacris',... 'The Red Light District', 2004, 4, ) cds(6) = makeCD( 'R & B', '2Pac',... 'Loyal To The Game', 2004, 3.9, ) cds(7) = makeCD( 'Rap', 'Eminem',... 'Encore', 2004, 3.5, ) cds(8) = makeCD( 'Heavy Metal', 'Rammstein',... 'Reise, Reise', 2004, 4.2, )

Recursive functions Before we conclude this chapter, we will discuss recursive functions, those that can call themselves. We have examples of functions that call other functions, e.g. insertionsort calling insert etc. If f(n) is defined in terms of f(n – 1), as for example, in the case of factorial, why not let f call itself? n! = n x (n – 1)! Or in matlab: fact(n) = n.* fact(n – 1)

Rules for recursive functions 1. there should be exit from recursion. (i.e., there should be some conditional branch path in which there is no recursive call). Such cases are called the base cases. 2. recursive calls should make towards base case (usually by calling itself with smaller input values). 3. Recursion may be more time or memory consuming so should be careful with their use.

Example 1: Write a recursive function to compute n! function out = fact(n) if n <= 1 out = 1; else out = n.* fact(n-1); end;

Example 2: Write a recursive function in Matlab to perform binary search. Assume array A is sorted in ascending order. Search(A, low, high, x) will return the largest t such that A(t) <= x. Pre-condition: A(low) <= x <= A(high) Thus low <= t <= high. Initially, low = 1, high = size(A)

Recursive binary search program function out = search(A, low, high, x) % returns the largest t s.t. A(t) <= x where low <= t <= high % A is assumed to be sorted if high - low == 1 if A(high) == x out = high; else out = low; end; else mid = floor((low + high)/2); if A(mid) == x out = mid; elseif A(mid) < x out = search(A, mid + 1, high, x); else out = search(A, low, mid - 1, x); end;

Binary search program function out = binary_search(A, x) if A(1) < x out = 0; else out = search(A, 1, length(A), x) end

Project: Generating all subsets of a given set Given a set, like [1, 3, 4], the subsets are: [ ] [1] [3] [4] [1 3] [1 4] [3 4] [1 3 4] We want to write a program to generate all the subsets of a given collection

Idea behind algorithm – recursion This is a problem for which non-recursive solutions are significantly harder than recursive solutions. Idea: input array is a of length n. Recursively find all subsets of a(2:n) Then add a(1) to each of the subsets. Combine the two collections.

Since we need to represent a collection of sets, we have two choices: 1. use of cell arrays 2. use of two-dimensional arrays The latter is not suitable for this problem since the sizes of the subsets are not the same We use recursion to generate the solution. We need a function insert that inserts a given number into all the sets of a given collection.

Example showing how insert works

Code for insert function out = insert(i, lst) % inserts i into each membet of lst for j = 1:length(lst) out{j}= [i, lst{j}]; end;

Code for subsets function L = subsets(lst) % generates all subsets of lst if length(lst) == 0 L = {[]}; elseif length(lst) == 1 L = {[lst(1)],[]}; else L1 = subsets(lst(2:end)); L2 = insert(lst(1), L1); L = [L1, L2]; end;

Printing the contents of a cell array function setprint(cellset) % prints every member of the cell in one line % assume cellset is a collection of sets of integers for k=1:length(cellset) aprint(cellset{k}); fprintf('\n'); end; function aprint(r) for j = 1:length(r) fprintf('%d', r(j)); fprintf(' '); end; fprintf('\n')

A recursive image tiling problem Given an input image as in the left-side, we want to produce the image on the right-side.

Solution to the recursive image tiling problem Observation 1: 3 rd quadrant has the input image, but shrunk into quarter. We need to write a function shrink that shrinks the image to quarter size. Quadrant 1 of the output is the shrunk version of the whole output. Equivalently, if F is the function that outputs the image on the right when given as input on the left, then F applied to the image on the 3 rd quadrant of the output is what goes into the 1 st quadrant of the output. Quadrants 2 and 4 are now easy to determine. They can be obtained by copying a part of the image in quadrant 1.