CS 99

Summer 2002: HW 9 Solutions                                                 07.24

Functions

 

1.    Perfect numbers revisited
function [ res ] = isperfect( p )
%ISPERFECT        True for perfect numbers.
%     ISPERFECT( P ) is 1 if P is a perfect number, 0 otherwise.
%     A perfect number equals the sum of its proper divisors, including 1 but not itself.
sumOfDiv  = 0;
for k = 1 : ( p – 1)
    if mod( p, k ) == 0
        sum = sum + k;
    end
end
res = ( sum == p );


2.    Time-conversion
Part A.

function [ seconds ] = hms2s( h, m, s )
%HMS2S    Converts a time expressed in hours, minutes, seconds to a time in seconds only.
%         HMS2S( H, M, S ) gives the number of seconds in H hours, M minutes, S seconds.
seconds = 3600*h + 60*m + s


Part B.  
function [ h, m, s ] = s2hms( secs )
%S2HMS    Converts a time expressed in seconds to an equivalent time in hours, minutes, seconds.
%          [H, M, S] = S2HMS( SECS ) gives the number of hours (H), minutes (M), and
seconds (S) in SECS seconds.
h = floor( secs/ 3600 );
secs = mod( secs, 3600 );
m = floor( secs/ 60 );
s = mod( secs, 60 );

 

3.    Palindromes
Write a function isPalindrome( w ) that takes an array of characters and returns true if the characters in the array constitute a palindrome, or false otherwise. A palindrome is the same forwards as backwards, e.g. ['a' 'b' 'c' 'b' 'a'].
function [res] = isPalindrome( w )
%ISPALINDROME     True for palindromes.
%      ISPALINDROME( W ) is 1 if W is a character string and a palindrome.  Spaces, periods, and capitalizations are ignored.
%          A warning is issued if W is not an array of characters.
if ischar( w )
    w = lower( w );
    w = w( w ~= ‘ ‘);
    w = w( w ~= ‘.’);
else
     warning(‘input should be a string’);
end
res = 1;
for pos = 1 : length( floor( pos/ 2) )
    if w( pos )  ~= w( end – pos + 1)
        res = 0;
        break;
    end
end
     

 

4.    Shifting Matrices
function [ M ] = upShift( M )
%UPSHIFT     Shifts the rows of its input matrix M 1 position upwards, moving the first row to the bottom and returns the up-shifted matrix as a result.
%                   UPSHIFT( M ) returns up-shifted version of M.
M = M( [2: end, 1], : );

 

5.     Summing lower triangles*

Write a function called sumDownTri that calculates the sum of the lower left triangular part of a rectangular matrix containing numbers.  The three sides of the triangle have the same number of elements.  Three examples of rectangular matrices are shown below.  The cells in the lower left of the triangle are labeled d and the remaining cells are labeled x.

                 d x x x x x x             d x x          d x x x
             d d x x x x x             d d x          d d x x
             d d d x x x x             d d d          d d d x
             d d d d x x x             d d d          d d d d
                                        d d d

Function sumDownTri takes on rectangular matrix M as the input argument and has one output argument triSum that contains the sum of the elements in the lower left triangular part of matrix M.  Assume the matrix is at least 2-by-2 in size.  Do not use the MATLAB predefined function tril.

function [ sumlow ] = sumDownTri( M )
%SUMDOWNTRI( M )     Returns the sum of the lower left triangular part of M.
[ r, c ] = size( M );
minDim = min( r, c );
if r > c
    start = r – minDim + 1;
else
    start = 1;
end
sumlow = 0;
for rows = start : r
    for cols = 1 : minDim
        sumlow = sumlow + M( rows, cols );
    end
end



*Challenge problem