UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ sample4

1. Swapping Values (Octave)

In this problem your task is to write an octave function swap that takes a matrix \(M\), numbers \(a\) and \(b\), and replaces all of \(a\)’s in M with \(b\)’s, and vice-versa. For full marks Your function should pass the tests below, and be fully vectorized, i.e. not use loops, cellfun or arrayfun.

%!test
%!shared M1, M2
%! M1 = [0,0;0,1;
%!       1,0;1,1];
%! M2 = [1,1;1,0;
%!       0,1;0,0];
%! assert(swap(M1,1,0) == M2);
%!assert (swap(M1,0,1) == M2);
%!assert (swap(M1,1,1) == M1);
%!test
%! M3=[1,2;
%!     3,4];
%! assert(swap(M3,1,4) == [4,2;
%!                         3,1])

1.1. Sample Solution

2. Go Territory (Octave)

In this problem, loosely based on the game of Go, your task is to find captured spaces: \(0\)’s in a matrix that have \(1\)’s above, below, left, and right. Write an octave function territory that takes \(0/1\)-valued matrix as input and returns \(1\) in the position of each captured space. For full marks your function should pass the tests below, and be fully vectorized, i.e. not use loops, cellfun or arrayfun

%!test
%! board = [1,0,1,0;
%!          0,1,0,1;
%!          1,0,1,0;
%!          0,1,0,1];
%! assert(territory(board) == [0,0,0,0;
%!                             0,0,1,0;
%!                             0,1,0,0;
%!                             0,0,0,0]);

%!test
%! board = [1,1,1,0;
%!          0,1,0,1;
%!          1,0,1,0;
%!          0,1,0,1];
%! assert(territory(board) == [0,0,0,0;
%!                             0,0,1,0;
%!                             0,1,0,0;
%!                             0,0,0,0]);

%!test
%! board = [1,0,1,0, 1,0,1,0;
%!          0,1,0,1, 0,1,0,1];
%! assert(territory(board) == zeros(2,8))

%!test
%! board = [1,0,1,0,1,0,1,0;
%!          0,1,0,1,0,1,0,1;
%!          1,0,1,0,1,0,1,0;
%!          1,0,1,0,1,1,1,0];
%! assert(territory(board) == [0,0,0,0,0,0,0,0;
%!                             0,0,1,0,1,0,1,0;
%!                             0,0,0,0,0,1,0,0;
%!                             0,0,0,0,0,0,0,0]);

3. Check Diet

Hint: Matrix times vector multiplication helps.

  1. Write an octave function checkdiet that checks if a proposed diet meets the minimum daily requirements. Your function should pass the given tests. For full marks your function should be fully vectorized (i.e. no loops).
  2. Identify two weaknesses of the given test suite for checkdiet and add two new tests.
## usage: passes = checkdiet(TABLE, MINS, DIET)
##
## Check if DIET passes the min daily requirements
##   TABLE(i,j) is the amount of nutrient i in food j
##   MINS(i) = minimum amount of nutrient i required
##   DIET(j) = amount of food j in proposed diet
function yesno = checkdiet(table, mins, diet)
end
%!test
%! assert(checkdiet(eye(3),ones(3,1),ones(3,1)) == 1)
%!test
%! assert(checkdiet(eye(3),zeros(3,1),[1;0;0]) == 1)
%!test
%! assert(checkdiet(eye(3),[1;0;0],zeros(3,1)) == 0)
%!test
%! assert(checkdiet(eye(3),[1;0;0],0.5*ones(3,1)) == 0)
%!test
%! assert(checkdiet(ones(3,3),[1;0;0],0.5*ones(3,1)) == 1)

4. Mincol

Write the octave function mincol that finds the position of the smallest element in each row of a matrix. You function should pass the given test. For full marks your function should be fully vectorized (i.e. no loops, arrayfun or cellfun).

## usage: given a matrix of integers, for each row
## return the column containing the minimum value
function out = mincol(data)

endfunction
%!test
%! A = [1,2,3;
%!     3,2,4;
%!     1,2,0;
%!     5,3,4;
%!     3,2,1;
%!     -10,0,0];
%! assert (mincol(A) == [1;2;3;2;3;1]);

4.1. Sample Solution

5. Neighbour Count

Complete the Octave function nbrcount that counts for each element of a zero/one matrix, how many of the neighbours are one. For full marks your solution should be vectorized.

function out = nbrcount(img)
endfunction
%!test
%! A=       [1,0,0;
%!           0,0,0;
%!           0,0,1;
%!           1,0,0];
%! counts = [0,1,0; 1,2,1; 1,2,0; 0,2,1]
%! assert(nbrcount(A) == counts)

5.1. Sample Solution

6. Isolated Pixels

Use the nbrcount function from the previous question to define a function isolated that finds all the isolated ones (those ones whose neighbours are all zero). For full marks your solution should be vectorized.

function out = isolated(img)
endfunction
%!test
%! A= [1,0,0; 0,0,0; 0,0,1; 1,0,0];
%! assert(isolated(A) == A)

%!test
%! A=[1,0,0;
%!    0,0,0;
%!    0,1,1;
%!    1,0,1];
%! assert(isolated(A) == [1,0,0; 0,0,0; 0,0,0;0,0,0])

6.1. Sample Solution

7. Normalize

Complete the vectorized Octave function normalize according to the given comment and tests. You do not need to copy the usage comment.

##usage: matrix = percent(raw, maxes)
##
## raw - raw scores, one row per student.
## maxes - maximum possible for that column
##
## Output is a matrix one row per student, with ratios
function out=normalize(raw, maxes)
endfunction
%!test
%! #    journal,assgn,  midterm,final
%! mxs=[260,    60,     20,     60];
%! nrm = [0.9, 0.9, 0.9, 0.9; 0.75, 0.75, 0.75, 0.75; 1, 0, 1, 0];
%! raw=[234, 54, 18, 54; 195, 45, 15, 45; 260, 0, 20, 0];
%! assert(normalize(raw, mxs), nrm, eps);

8. Percent

Use the function normalize from the previous question to complete the following vectorized Octave function to calculate final percentages for students in a class. You do not need to copy the usage comment.

##usage: scores = percent(raw, maxes, weights)
##
## raw - raw scores, one row per student.
## maxes - maximum possible for that column
## weights - weight for that column in percent
##
## Output is a column vector of a percent for each student.
function out=percent(raw, maxes, weights)
end
%!test
%! #    journal,assgn,  midterm,final
%! mxs=[260,    60,     20,     60];
%! wgt=[20,     30,     20,     30];
%! raw=[234,    54,     18,     54;
%!      195,    45,     15,     45;
%!      260,    0,      20,     0;
%!      0,      60,     0,      60;
%!      200,    40,     17,     33];
%! assert(percent(raw, mxs, wgt), [90;75;40;60;68.88], .01);

9. Digits2num

Write a function to convert the digits of a positive base 10 number to the corresponding number. Your function should pass the following tests.

%!test "0"
%! assert(digits2num([0]) == 0);
%! assert(digits2num([0,0]) == 0;)

%!test "1"
%! assert(digits2num([1]) == 1);
%! assert(digits2num([0,1]) == 1;)

%!test "place value"
%! assert(digits2num([2]) == 2)
%! assert(digits2num([2,0]) == 20)
%! assert(digits2num([2,3,0]) == 230)

9.1. Sample Solution