Review questions for Test 2B ----------------------------- 1. Longest function [len, ind] = longest(C) % Find the longest string(s) in cell array C. % C is an n-by-1 cell array of strings, n>=1. % len is the length of the longest string in C. % ind is a vector, possibly of length one, containing the index number(s) % of the string(s) with length len 2. BigTriplets function N = BigTriplets(A) % Suppose A is a length-50 array of State objects with two properties. % Assume A(k).name is a string that names a state and A(k).pop % is an integer whose value is the states population. % We say that three different states form a "big triplet" if % the sum of their populations is greater than 15 million. % N is the number of big triplets. 3. Above the median function C = topHalf(A) % A is a 1-d array of State objects; each object has two properties: name and pop. % A(k).name is a string that names a state. % A(k).pop is an integer whose value is the states population. % The states in A are ordered alphabetically by state name. % C is a 1-d cell array of strings that names all the states whose % populations are above the median state population. % The states should be ordered alphabetically in C. % % Do NOT use vectorized code. % % Hint: Function median returns the median value of a NUMERIC vector. 4. Point class % Given an array of Point objects Pts where each object has two properties, % x and y, sort Pts so that the objects are in the order of % increasing distance from (0,0) 5. Implement function partialStrings as specified. Do not use vectorized code. function M = partialStrings(CA, t) % CA is a length n cell array of "strings"; a "string" is a row char vector. % t is a positive integer. % M is an n-by-t matrix of characters: % Row r of M contains the first t characters of the rth char vector in CA. % If the rth char vector in CA is shorter than t, pad the char vector with ’!’. % NO VECTORIZED CODE For example, let cell array CA hold these five char vectors: 'd' 'a2c' '# _!.a' '' 'ap' Then the function call M = partialStrings(CA,3) should return a matrix M like this: ['d!!' ; ... 'a2c' ; ... '# _' ; ... '!!!' ; ... 'ap!'] 6. (a) Implement this function: 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 object with the following public properties: % x: x-coordinate of center of disk % y: y-coordinate of center of disk % radius: radius of disk (b) Implement the following function to return the indices of disk triplets that overlap. Three disks form a triplet if every disk overlaps with each of the other two. Make effective use of function overlap from part (a). Your code should be efficient—avoid unnecessary iterations. function idx = diskTriplets(D) % D is a 1-d array of disk objects; each objects has properties 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. 7. (OOP) Refer to class Interval from Lecture 24. Implement the following function: function idxs = greatestOverlap(iArray) % Find the biggest pairwise overlap between Intervals in iArray. % iArray is an array of Interval references. iArray has a length greater than 1. % idxs is a vector of length 2 storing the indices of the two Intervals in % iArray that overlap the most. If there is not a pair of overlapping % Intervals in iArray, idxs is the empty vector. % Write efficient code: avoid unnecessary iteration. 8. (OOP) Implement the following class as specified: classdef LengthUnit < handle % A LengthUnit represents a length (distance) in imperial units: feet and % inches. The values are non-negative and inches must be less than 12. properties feet % a non-negative integer value inches % a non-negative value end methods function L = LengthUnit(ft, in) % Constructor: Create a LengthUnit object with ft feet and in inches. % If one or both parameters are negative, halt the program and % display an error message. %--Write your code here end function ft = inFeet(self) % self references a LengthUnit. ft is self represented in feet. %--Write your code here end function L = add(self, other) % self and other reference LengthUnits. L references a new % LengthUnit that is the sum of self and other. %--Write your code here end function yesno = isLongerThan(self, other) % self and other reference LengthUnits. yesno is true (1) if % self is longer than other; otherwise yesno is false (0). % For full credit, make effective use of method inFeet. %--Write your code here end end %methods end %classdef 9. (OOP) Write a script that (a) generates 10 values, each uniformly random in (0,50). Let these random values be lengths in inches and use an array of LengthUnit objects to store these lengths. (b) determines which LengthUnits are shorter than or equal in length to the first LengthUnit in the array; display their indices and their lengths in feet. Make efficient use of available methods in class LengthUnit. 10. Questions from "Insight": P8.1.5 (Clarification: y is a row vector that is the left shift of x) P8.1.8 P9.1.4 (do not use function `deblank`) P9.1.7 (do not use built-in functions strfind, findstr, or find) P9.2.5