Review questions for Prelim 3 ----------------------------- 1. allInterval function B = allInterval(x,a,b) % x is a length-n vector. % B is assigned the value of 1 (true) if every x-value is in the % interval [a,b]. % B is assigned the value of 0 (false) if at least one of the x-values % is not in the interval [a,b]. % Your implementation should make effective use of a while-loop. 2. TotalValue % Refer to the Cost-Inventory application in Lecture 13. % Complete the following function... function T = totalValue(Inv,Cost) % T is the total value of all the inventory in all the factories 3. Censoring text % Refer to Lec 17 on characters and strings and implement this function: function D = censor(str, A) % Replace all occurrences of string str in character matrix A, regardless % of case, with 'X's. % A is a matrix of characters. % str is a string. Assume that str is never split across two lines in A. % D is A with 'X's replacing the censored string str. % % Example: A = ['Use MATLAB '; ... % 'in that lab.'] % % and str = 'lab' % % Then D = ['Use MATXXX '; ... % 'in that XXX.'] 4. TwoClicks % Refer to Lecture 13 and the function RandomLinks(n). A = RandomLinks(1000); % Note that if A(i,j) is one, then there is a link on webpage j % to webpage i. Write a fragment that prints "yes" if it is % possible to go from web page #100 to webpage #200 in one or two clicks. % Thus, if A(101,100) = 1 and A(200,101) = 1 then "yes". 5. Transpose % Refer to Lecture 21 and the function TheDigits: C = TheDigits(); % C is a length 10 cell array with the property that C{k} is % a 7-by-5 bitmap of the digit k (regarding 0 as the "tenth" digit.) % Produce a length 10 cell array D which houses 5-by-7 bitmaps % of the "transposed" arrays. Thus, since % % C{1} looks like % 00100 % 01100 % 00100 % 00100 % 00100 % 00100 % 01110 % then D{1} should look like % % 0000000 % 0100001 % 1111111 % 0000001 % 0000000 % % Do not use the transpose operator ' 6. Checking for numbers in a vector/matrix function B = vecHasNegAndPos(x) % x is a length-n vector and n>=2 % B is assigned the value of 1 (true) if x has at least one component % that is strictly negative and at least one component that is % strictly positive. % Otherise B should be assigned the value of 0. % Your implementation should make effective use of a while-loop. function B = matHasNegAndPos(A) % A is an m-by-n real array with M>=2 and n>=2 % B is assigned the value of 1 (true) if A has at least one component % that has a strictly negative value and at least one component that has a % strictly positive value. % Otherise B should be assigned the value of 0. % Your implemention should make effective use of VecHasNegAndPos 7. Reduce function B = Reduce(A) % A is an n-by-n array with n odd and at least 3 in value. % B is obtained by deleting all the even-indexed rows and columns. % Thus if % A = [ 1 2 3 4 5 ;... % 6 7 8 9 10 ;... % 11 12 13 14 15 ;... % 16 17 18 19 20 ;... % 21 22 23 24 25 ] % then % B = [ 1 3 5;... % 11 13 15;... % 21 23 25] 8. 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 9. TopHalf function C = topHalf(A) % Refer to Lecture 21. % Suppose A is a length-50 structure array with two fields % Assume A(k).name is a string that names a state and A(k).pop % is an integer whose value is the states population. % Assume that the states are ordered alphabetically. % C is a length-25 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. % Obtain a vector of populations... pop = zeros(50,1); for k=1:50 pop(k) = A(k).pop; end [y,idx] = sort(pop); % Note that idx(k) is the index of the kth smallest state. % Above-the-median states have indices idx(26),..,idx(50). % Put these indices in a single, length-25 vector Above = idx(26:50); % Without the following step the "big" states wont be assembled in alphabetical % order... Above = sort(Above); % This assembles the state names in C... C = cell(25,1); for k=1:25 j = Above(k); str = A(j).name; C{k} = str; end 10. BigTriplets function N = BigTriplets(A) % Refer to the Lectures 20 and 21. % Suppose A is a length-50 structure array with two fields. % 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. 11. Point struct % Given a structure array Pts where each structure has two fields, x and y, % sort Pts so that the structures are in the order of % increasing distance from (0,0) 12. Questions from "Insight": M7.1.3 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 or find) P9.1.9 (count lower case letters only) P9.2.5