tuesday, 5/2 note: this was a fun, gee-whiz lecture to show you the sorts of things you are now able to do in matlab. you are not responsible for new matlab commands shown in this lecture that were not shown earlier, e.g. you are NOT responsible for $clf$, $treeplot$, $deblank$, $blanks$, $patch$, $pcolor$, $image$, or $imagesc$. % ideas from prelim 3, question 1: make a checkerboard clf; x = zeros(5, 8); [h,w] = size(x); x(1:2:h, 1:2:w) = 1; x(2:2:h, 2:2:w) = 2; pcolor(x); % note: pcolor does not draw the last row or last column :( % prelim 3, question 2: see newsgroup % prelim 3, question 3: overlapping rectangles clf; n = 5; width = ceil(rand(1,n) * 50); height = ceil(rand(1,n) * 50); left = ceil(rand(1,n) .* (100 - width)); right = left + width; bottom = ceil(rand(1,n) .* (100 - height)); top = bottom + height; pixels = zeros(101,101); for i = 1:n pixels(bottom(i):top(i), left(i):right(i)) = pixels(bottom(i):top(i), left(i):right(i)) +1; % note: typo during lecture: the $(i)$s above were omitted end imagesc(pixels) % draw a tree % 9 % /|\ % 6 7 8 % /|\ \ % 2 3 4 5 % | % 1 % % 1 2 3 4 5 6 7 8 9 t=[5 7 7 7 8 9 9 9 0]; % t(i) = "parent" of i; 0 means "no parent" clf; treeplot(t); % right justify rectangular array of text a = strvcat(... 'right justify text in matlab', ... 'matlab trees', ... 'triangle of stars', ... 'overlap rectangle', ... 'exercise 9 -- duplicate birthdays', ... 'dutch national flag'); [h,w] = size(a); for i = 1:h s = deblank(a(i,:)); a(i,:) = [blanks(w-length(s)) s]; end a % exercise 9 -- duplicate birthdays clf; npeople = 500; ndays = 365; ntrials = 1000; counts = zeros(1,ntrials); for i = 1:ntrials days = ceil(rand(1,npeople) * ndays); counts(i) = max(full(sparse(1,days,1,1,ndays))); end hist(counts, 1:10); % triangle of stars clf; x = [ 15 25 50 ]; y = [ 10 40 60 ]; x = x - min(x) + 1; y = y - min(y) + 1; % shift coord to be >= 1 patch(x,y,'r'); pause width = max(x); height = max(y); xs = 1:width; xs = xs(ones(1,height),:); ys = (1:height)'; ys = ys(:,ones(1,width)); inside = ones(height,width); for i = 1:3 clf; hold on v = [x ; y]; w = v(:,i); v(:,i) = []; % save and delete i-th vertex % want equation Ax + By + C = for line through points in V A = v(2,2) - v(2,1); B = v(1,2) - v(1,1); B = -B; C = - (v(1) * A + v(2) * B); side = (A * xs + B * ys + C <= 0) == (A * w(1) + B * w(2) + C <= 0); inside = inside & side; pcolor(side); plot([x x(1)], [y y(1)], 'r') pause % wait for user to press a key end stars = ' *'; inside = stars(inside+1); disp(flipud(inside)) % draw, making increasing y/row go up, not down