Chapter 8 while loop syntax while condition statement

  • Slides: 32
Download presentation
Chapter 8 while loop syntax while <condition> <statement>; . . . end; Loops

Chapter 8 while loop syntax while <condition> <statement>; . . . end; Loops

Example 1: We wrote a function insert that inserts A(k) in the array A(1:

Example 1: We wrote a function insert that inserts A(k) in the array A(1: k – 1). Now we will write the function insertionsort to call insert iteratively. function insert(A, k) % assume k > 1 and A(1: k-1)is sorted temp = A(k); j = k-1; while (j > 0) if A(j) > temp A(j+1) = A(j); j = j – 1; else break; end; A(j+1) = temp;

Insertion sorting function B = insertion. Sort(A) j = 2; while (j <= length(A))

Insertion sorting function B = insertion. Sort(A) j = 2; while (j <= length(A)) B = insert(A, j); j = j + 1; end; Comparison between insertion sorting and selection sorting • In the best case, insertion sorting is much faster than selection sorting. • On average, insertion sorting performs half as many comparisons as selection sorting. • Selection sorting is much better than insertion sorting in the number of data movements.

for loop syntax for j = 1: k <statement sequence> end

for loop syntax for j = 1: k <statement sequence> end

An example involving image processing Median filter: Suppose an image is corrupted by a

An example involving image processing Median filter: Suppose an image is corrupted by a random noise that randomly changes a small number (say 10%) of pixels to arbitrary value. How can we remove this noise? Sample input and output:

Median filter design We should avoid trying to determine which pixels are corrupted. (too

Median filter design We should avoid trying to determine which pixels are corrupted. (too difficult. ) Instead, replace the content of each pixel by some combined value of the neighbors. Good pixel: likely surrounded by nearly identical values, so replacement does not hurt. Bad pixel: most (or all) neighbors are good so replacement removes the noise! How to combine the neighbor values? • mean filter median filter

Median filter implementation function out = median. Filter(img) [row, col, num]= size(img); for i

Median filter implementation function out = median. Filter(img) [row, col, num]= size(img); for i = 2: row - 1 for j = 2: col-1 lst 1 = sort([img(i-1, j-1, 1), img(i, j, 1), img(i+1, j+1, 1)]); out(i, j, 1) = lst 1(5); lst 1 = sort([img(i-1, j-1, 2), img(i, j, 2), img(i+1, j+1, 2)]); out(i, j, 2) = lst 1(5); lst 1 = sort([img(i-1, j-1, 3), img(i, j, 3), img(i+1, j+1, 3)]); out(i, j, 3) = lst 1(5); end; img(i-1, j, 1), img(i-1, j+1, 1), img(i+1, j-1, 1), img(i-1, j, 2), img(i-1, j+1, 2), img(i+1, j-1, 2), img(i-1, j, 3), img(i-1, j+1, 3), img(i+1, j-1, 3),

Simulating the outcome of a probability game Players A and B play the following

Simulating the outcome of a probability game Players A and B play the following game. A fair coin is tossed until one of the two patterns HTH or THT occurs. If the first pattern occurs, A wins else B wins. What is the probability that A wins the game? There are two ways to solve this problem: • Use Markov chain (can find the answer exactly. Will be studied later. ) • Simulation (simulate the experiment a large number of times and record the number of wins by each player)

Simulation based solution Outer loop: repeat the game a large number of times, say

Simulation based solution Outer loop: repeat the game a large number of times, say 100000. Inner loop: each iteration represents one game. Outline of solution: count. A = 0; count. B = 0; for j = 1: 1000000 while the game is not over toss coin once; update the last three bits; end; update the winner count; end;

We will write three functions: toss() produces 1 or 2 with probability 0. 5

We will write three functions: toss() produces 1 or 2 with probability 0. 5 tossgame() produces 1 (2) if A (B) is the winner of one randomly simulated game repeat. Toss. Game(n) repeat the tossgame n times, find a = the number of times A wins and output the ratio a/n

Recursive functions Before we conclude this chapter, we will discuss recursive functions, those that

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

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

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

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

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;

A recursive image tiling problem Given an input image as in the left-side, we

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

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.

Some exercises in Chapter 8 and solutions Exercise 8. 1. The input is a

Some exercises in Chapter 8 and solutions Exercise 8. 1. The input is a two-column matrix. The first column gives the number and the second column gives the number of repetitions. You are to create an output vector that matches these numbers. For example: >> repeat([2 3; 3 1; 4 2] ans = [222344]

Exercise 8. 1. >> repeat([2 3; 3 1; 4 2] ans = [222344] function

Exercise 8. 1. >> repeat([2 3; 3 1; 4 2] ans = [222344] function out = repeat 1(m) out = []; [r, c] = size(m); for i = 1 : r %Go through each row for j = 1 : m(i, 2) %Repeat the number of times specified in the second column out(end + 1) = m(i, 1); end

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s.

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s.

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s. >> A = [9, 11, 8, 6, 11, 12]; >> myfind(A, 11) ans = [ 0 1 0]

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in

Exercise 8. 4. Write a function myfind, that mimics the behavior of the built-in function find. That is, it takes as input a boolean vector of 1’s and 0’s. function out = myfind(A, x) out = []; for j = 1: length(A) if A(j) == x out = [out, j]; end;

Exercise 8. 8 Write a function into. Bits to take an integer number as

Exercise 8. 8 Write a function into. Bits to take an integer number as input and output a string of 0’s and 1’s representing the number in base 2. >> into. Bits(12) ans = 1100

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find the overall minimum, column minima and row minima of a matrix A.

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find the overall minimum, column minima and row minima of a matrix A. function res = min. All(m) [r, c] = size(m); res = m(1, 1); %Set up the first temporary minimum for i = 1 : r %Going through each row for j = 1 : c %Going throuch each column if res > m(i, j) %Updating if necessary res = m(i, j) end end

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find

Exercise 8. 11 Write functions min. All, min. Col and min. Row that find the overall minimum, column minima and row minima of a matrix A. function res = min. Row(m) [r, c] = size(m); res = zeros(1, r); %Set up res for i = 1 : r %Going through each row res(i) = m(i, 1); %Set up the first temporary minimum for row i for j = 2 : c %Going throuch each column if res(i) > m(i, j) %Updating if necessary res(i) = m(i, j); end end

Project – entropy computation The concept of entropy is fundamental to information and coding

Project – entropy computation The concept of entropy is fundamental to information and coding theory – which deals with efficient ways to add redundancy to the data so that the receiver can recover the message even if some of data is corrupted by the channel. Information theory was developed by Claude Shannon. Watch movie about Shannon in UCTV. http: //www. uctv. tv/search-details. aspx? show. ID=6090

probability distribution of individual letters The basic idea of information theory is that if

probability distribution of individual letters The basic idea of information theory is that if the input has less randomness, it can be more easily predicted and hence it can be compressed to greater degree. Suppose there are k distinct symbols in a text. Let P(j-th symbol) = pj. Entropy is defined as: E= – Sj pj log pj Goal is to compute the entropy of a given text.

Example: Given a three-letter alphabet containing E, Z and _ with frequencies as follows:

Example: Given a three-letter alphabet containing E, Z and _ with frequencies as follows: E Z _ 0. 5 0. 25 Entropy is given by: E = -0. 5 log 2 0. 5 – 0. 25 log 2 0. 25 Using matlab, we can compute E as: >> p = [0. 5 0. 25] p = 0. 5000 0. 2500 >> -sum(p. *log 2(p)) ans = 1. 5000 0. 2500

Project: Write a program in Matlab that computes the entropy of English. • Step

Project: Write a program in Matlab that computes the entropy of English. • Step 1: collect a corpus of text. count the frequency of letters for the corpus. • text will be stored in multiple files • program should calculate the frequency of letters across all the files. • Step 2: compute entropy from the frequency table.