Agenda 8 March 2013
Spring Ahead! (Time Change)
Test – Looks good so far!
Last Word on Structure Arrays (see below)
Recursion is a big deal
Tell Us a Story
General Says, It was a dark rainy night and the men were gathered in the mess hall after Battle, General Says, Young Colonel tell us a story
Factorials
Fibonacci
Palindrome
But how does it work (Use of a Stack)
Cafeteria
Golden Corral
What does Recursion Require?
1. Termination Condition
2. Call the Clone (same function)
3. Move towards Termination Condition
****** Function fact *******
function res = fact(N)
if N < 0 || mod(N,1) > 0
error('no, not a good number...') % called a wrapper
else
res = rfact(N);
end
end
**** Real Function *****
function res = rfact(N)
% 1. have a terminating condition
if N == 0
res = 1;
else
% 2. call a clone of the original function
% vvvvv
res = N * rfact(N-1);
% ^^^
% 3. parameter(s) of that call must move towards
% the terminating condition
end
end
***** Darn those Rabbits ******* Text Chapter 9 *****
****** Fibonacci Series ******
function res = fib(N)
% Recursion of the Nth Fibonacci
if N == 1 || N == 2
res = 1;
else
res = fib(N-1) + fib(N-2);
end
end
****** Is a Palindrome *******
zerorez
never odd or even
function YesNo = isPal(str)
% recursive palindrome detector
if length(str) < 2
YesNo = true;
elseif str(1) ~= str(end)
YesNo = false;
else
YesNo = isPal(str(2:end-1));
end
end
****** Example: Structures from Spreadsheet *********
function im_really_a_script
str = get_grades('gradeSheet.xls', 2, 'L'-'A', 2);
% display the structures
for st = str
st
end
end
function stra = get_grades(name, hdrrow, cols, left)
% field names are on row 'hdrrow' of the text file:
% there are 'cols' fields to process
% there are 'left' tezxt columns to the left of the nums array
clc
% initialize the structure array to empty
[nums txt ~] = xlsread(name);
[nrows ~] = size(nums);
for student = 1:nrows
str = struct();
for col = 1:cols
% extract the field name from the header row
name = txt{hdrrow, col};
% determine if this field contains a vector
[numeric, field, ndx] = has_num(name);
% for each student
% find the row in the txt array
row = hdrrow + student;
% get a numeric value from the nums array
if numeric
value = nums(student, col - left);
% get a text value from the txt array
else
value = txt{row, col};
% if the string is empty, it's really a non-indexed number
if length(value) == 0
% so get its value from the nums array
value = nums(row - hdrrow, col - left);
end
end
% insert the value in the right place in the structure:
%
% if the field exists and is numeric, put the value
% in the correct index position in the field
if isfield(str, field)
% fetch the previous field contents
now = str.(field);
% add the new value
now(ndx) = value;
% put it back in the field
str.(field) = now;
% otherwise, just put the value whatever it is in the field
else
str.(field) = value;
end
end
stra(student) = str;
end
end
******* find a number ************
function [res, field, ndx] = has_num(name)
res = false;
field = name;
ndx = 1;
n = find(name >= '0' & name <= '9');
res = length(n) > 0;
if res
num = name(n);
ndx = str2double(num);
field = name(1:n(1)-1);
end
end