Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)

Similar presentations


Presentation on theme: "Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)"— Presentation transcript:

1 Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)

2 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]

3 Cell arrays

4 Cell array – examples

5 Cell – operations

6 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'

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

8 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)

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

10 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;

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

12 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;

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

14 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

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

16

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

18 Example showing how insert works

19 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;

20 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;

21 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')

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

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


Download ppt "Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)"

Similar presentations


Ads by Google