Solutions to some old exam questions ------------------------------------ %Question 1 function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal's triangle % % (Mostly) Non-vectorized version p= 1; % values in level 0 of Pascal's triangle for currentLevel= 1:lev % Temporary vec (q) for current level is one cell longer than prev level q= ones(1,currentLevel+1); % Use the general formula for INTERNAL entries for i= 2:length(p) q(i)= p(i-1) + p(i); end p= q; % Update p end % p=q is actually vectorized. Truly non-vectorized code is: % for i= length(q):-1:1 % p(i)= q(i); % end ---------------------------------- function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal's triangle % % First build a Pascal's MATRIX, then extract the row m= ones(lev+1, lev+1); for currentLevel= 1:lev for c= 2:currentLevel % "interior" entries only m(currentLevel+1, c) = m(currentLevel, c-1) + m(currentLevel, c); end end p= m(lev+1, :); % p is just the last row of pascal matrix ---------------------------------- function p = pascalVector(lev) % p is the vector corresponding to level lev of Pascal's triangle % % Vectorized version p= [1]; % values in level 0 of Pascal's triangle for currentLevel= 1:lev p = [p 0] + [0 p]; % each level is one cell longer, so shift and add end ------------------------------------ ------------------------------------ %Question 2 function MyHistogram(v) % Draw a histogram for the data in v using asterisks in the COMMAND WINDOW. % v is a vector of non-negative values. % The histogram is scaled so that the largest data value is represented by % ten asterisks. Round as necessary in order to draw whole asterisks. % Example: v = [12 4.1 0.5 9.2 20] % Output in Command Window: % ****** % ** % % ***** % ********** multiplier= 10/max(v); scaledV= round(multiplier*v); for r= 1:length(v) astr= ' '; for c= 1:scaledV(r) astr= [astr '*']; end disp(astr) end ------------------------------------ ------------------------------------ %Question 3 function alfa = isIn(x, v) % alfa is 1 if value x is in vector v. Otherwise alfa is 0. % x is an integer. v is a vector of integers, possibly of length 0. % If v has length 0 (v is the empty vector), then alfa is 0. alfa= 0; k= 1; while k<=length(v) && alfa==0 alfa= x==v(k); k= k+1; end function s = intersectionSet(a,b) % a and b are vectors of integers. a and b are not empty. % Vector s contains only the values that are in both vectors a and b. % Vector s contains distinct values. s may be empty. s= []; for k= 1:length(a) if ~isIn(a(k),s) && isIn(a(k),b) s= [s a(k)]; end end ------------------------------------ ------------------------------------ %Question 4 function z = overlap(diskA, diskB) % z is 1 (true) if diskA and diskB overlap; otherwise z is 0 (false). % diskA and diskB are each a disk structure with the following fields: % x: x-coordinate of center of disk % y: y-coordinate of center of disk % radius: radius of disk dis = sqrt((diskA.x - diskB.x)^2 + (diskA.y - diskB.y)^2); if dis < diskA.radius + diskB.radius z = 1; else z = 0; end function idx = diskTriplets(D) % D is a 1-d array of disk structures; each structure has fields as defined in % part (a). Assume D has a length greater than 3. % idx is a vector of indices indicating all triplet overlap combinations. For example, % if disks 2, 4, and 5 form a triplet and disks 3, 4, and 6 form a triplet, idx % should be the vector [2 4 5 3 4 6]. Other orderings of triplets are acceptable, % however each triplet should only appear once. n = length(D); idx = []; for i = 1:n - 2 for j = i + 1:n - 1 for k = j + 1:n if overlap(D(i), D(j)) && ... overlap(D(j), D(k)) && overlap(D(i), D(k)) idx = [idx, i, j, k]; end end end end