Review questions for prelim 2

  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 14.
    % 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

    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.']
    %
    % Hint: function strcmpi does case-insensitive comparison of two strings.
    % Examples:  strcmpi('LAB', 'lab') returns true
    %            strcmpi('LAB', 'LA')  returns false
  4. TwoClicks

    % Refer to Lecture 14 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. 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
  6. 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]
  7. 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
  8. Questions from Insight