We Want to Make This Structure

We Want to Make This Structure

<p>% Agenda -Structure Arrays -Recursion 2D-Plotting</p><p>%%% We want to make this Structure %%%%%%%%%%%%% ans = </p><p>1x46 struct array with fields: TW LW Title Studio Weekend_gross Weekend_change Theater_count Theater_change Average Gross Budget Week %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function mv = make_movies clc fh = fopen('movies.txt','r'); line = fgetl(fh); mv = []; while ischar(line) line = fgetl(fh); if ischar(line) mv = [mv make_movie(line)]; end end end</p><p>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function mv = make_movie(line) % Header Record % TW|LW|Title|Studio|Weekend|Gross|%Change| ... % Theater|Count|Change|Average|Total Gross|Budget|Week| % Typical Data % 1|2|Identity Thief|Uni.|$14,064,000|-40.6%|3,222|+57|$4,365|$93,667,000| $35|3 % Both tab delimited [token line] = strtok(line, char(9)); mv.TW = getNum(token); [token line] = strtok(line, char(9)); mv.LW = getNum(token); [token line] = strtok(line, char(9)); mv.Title = token; [token line] = strtok(line, char(9)); mv.Studio = token; [token line] = strtok(line, char(9)); mv.Weekend_gross = getNum(token); [token line] = strtok(line, char(9)); mv.Weekend_change = getNum(token); [token line] = strtok(line, char(9)); mv.Theater_count = getNum(token); [token line] = strtok(line, char(9)); mv.Theater_change = getNum(token); [token line] = strtok(line, char(9)); mv.Average = getNum(token); [token line] = strtok(line, char(9)); mv.Gross = getNum(token); [token line] = strtok(line, char(9)); mv.Budget = getNum(token); [token line] = strtok(line, char(9)); mv.Week = getNum(token); end</p><p>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function res = getNum( str ) str = str(str ~= ' ' & str ~= '$' & str ~= ',' & str ~= '%' & str ~= '+'); if length(str) == 1 && (str(1) == '-' || str(1) == 'N') res = NaN; else res = str2double(str); end end</p><p>%%%%%%%%%%%%%%% Movie Questions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear clc mv = make_movies % total gross take this weekend gross = [mv.Weekend_gross]; gross = gross(~isnan(gross)); %skips the NANs sumMills = sum(gross) ./ 1000000; fprintf('total take was $%5.3f million\n\n\n', sumMills) % rank movies by Average field (gross per theater) avgs = [mv.Average]; avgs = avgs(~isnan(avgs)); %skips the NANs [~, order] = sort(-avgs); fprintf('ord\ttheaters\tname\n') for ndx = 1:8 mvndx = order(ndx); it = mv(mvndx); fprintf('%d\t\t%d\t%s \n', ndx, ... it.Theater_count, it.Title) end</p><p>****** 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</p><p> clc</p><p>% 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</p><p>% 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</p><p>******* 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</p><p>Recursion</p><p>Recursion is a big deal</p><p>Tell Us a Story</p><p>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</p><p>Factorials Fibonacci Palindrome But how does it work (Use of a Stack) Cafeteria Golden Corral</p><p>What does Recursion Require? 1. Termination Condition 2. Call the Clone (same function) 3. Move towards Termination Condition</p><p>****** 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</p><p>**** 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</p><p>****** 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</p><p>2D-Plotting ______Basic Plotting______</p><p>----2-D Plots ----- together x = linspace(-1.5, 1.5, 30); y1 = x; y2 = x.^2; y3 = x.^3; y4 = x.^4; plot(x,y1,x,y2,x,y3,x,y4)</p><p>----2-D Plots ----- separate clf x = -2*pi:.05:2*pi; subplot(2,3,1) plot(x,sin(x)'); title('1 - sin(x)') subplot(2,3,2) plot(x,cos(x)) title('2-cos(x)'); subplot(2,3,3) plot(x, tan(x)); title('3-tan(x)'); subplot(2,3,4) plot(x,x.^2) title('4-x^2')</p><p>----- 2D Plots – </p><p>______example curves ______clear clc close all th = linspace(0, 6*pi, 210); p1 = sin(th); plot(th, p1) hold on plot(th, cos(th), 'r--') t1 = tan(th) %t1(abs(t1) > 2) = NaN; plot(th, t1, 'g.') axis([0 20 -2 2]) legend({'sin(th)','cos(th)', 'tan(th)^2'})</p><p>______3-D Plots ______clear clc close all</p><p>% plot a function x = linspace(-4*pi, 4*pi, 200); y = sin(x); z = x .^ 2; plot3(x, y, z) hold on plot3(x, y, z, 'ro') grid on xlabel('x') ylabel('sin x') zlabel('x^2') % parametric plot (two dependent variables) figure th = linspace(0, 2*pi); plot(cos(th), sin(th)) axis equal</p><p>______MyTree with Wrapper ______function myTree hold on treeR(0,0,10,0) end function treeR(x, y, ln, th) x1 = x + ln .* sin(th); y1 = y + ln .* cos(th); if ln < 0.15 plot([x x1], [y y1], 'g') else plot([x x1], [y y1], 'k') rn = rand(1,3) - 0.5; newL = ln * 0.7; %try 0.6 treeR(x1, y1, newL, th+rn(1)) treeR(x1, y1, newL, th+rn(2)) %treeR(x1, y1, newL, th+rn(3))</p><p> end end</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us