Comments on Prelim 2

If you have done well, congratulations! If you have not done well, be sure to catch up and then show us at the final exam what you have learned! Be sure to re-do any problem on which you did poorly, perhaps with the help of a member of the course staff. Do not just read the solution as that won't help you learn for real.

Question 1 (array vs. cell array, char and strings) :-(

This short problem was the most challenging question on the prelim, if one judges by the per-question score! In a simple array, each "bin" can store only one scalar, for example, one character, but not a string. In a cell array, each "bin" can store something bigger than one scalar, for example, a vector. A string is a vector; it is a vector of characters. In terms of syntax... use square brackets to create a simple array; use curly brackets to create a cell array.

Question 2 (while-loop pattern, characters and strings) :-|

Students did well at using strcmp to compare the first 4 positions in s with ‘Hola’. However, most students forgot that prior to accessing s(1:4) they needed to check that the length of s was greater than or equal to 4. Without this check, empty or short lines would cause the code to crash--index out-of-bound error. This was the most common error.

Most students also correctly identified that another boolean expression needed to be added to the while loop to stop looping once a match to ‘Hola’ was found. This was important for the efficiency of the code, but even more important was that it also prevented you from getting the last occurrence of ‘Hola’ instead of the first.

Most students correctly wrote an if-else statement after the loop to choose between printing 'No' and the number of lines ABOVE the first occurrence of 'Hola'. Here's a common off-by-one error: if you always increment the line count inside the loop--even during the last iteration when 'Hola' is found--then the number of lines ABOVE 'Hola' is one fewer than your line count.

Question 3 (full traversal of 2-d array vs. traversing a triangular half, building up histogram data) :-)

Most students recognized that one could efficiently solve the problem by traversing the 2-d array M and for each component of M increment the appropriate bin in vector data; this involves two loops nested for matrix traversal. A very inefficient solution would use three nested loops: one loop to iterate through data and at each bin search through the matrix M using two nested loops for the values that should be counted at that bin of data.

Since M(r,c) is the time to city r from city c, "traveling to and from the same city" corresponds to the components of the matrix where the row index r is equal to the column index c--the main diagonal. To exclude those components, simply check that r~=c as you traverse the matrix using nested loops over all row numbers r and all column numbers c. You should not assume that M had to contain zeros on the main diagonal--the question said nothing about how the trips were timed so you should not make assumptions about the values in M beyond what was stated in the question!

In part (b), when "the trip (r,c) and the trip (c,r) are considered to be the same and are counted as one trip," you needed to traverse only half of the matrix, either above the main diagonal or below.

Question 4 (2-d simple array, calculating indices) :-) :-)

Very well done in general! A few students had trouble dealing with the extra columns in matrix W after interleaving up to the width of the narrower matrix U. If you didn't do well here, review Lab Exercise 10 (the shuffling cards question) and then try this prelim question again. Don't just read the solution.

Question 5 (3-d array, computing with type uint8) :-)

This question was generally well done. Most students knew how to carry out arithmetic in type uint8. Conceptually, students understood the problem and what they needed to do but some stumbled with the implementation:

(1) Some students wrote separate loops to handle the interior case and the edge case separately, which was completely correct, but this involved a lot of extra writing--more opportunity for errors. Although the interior case dealt with each layer independently while the edge case required that one calculate across the layers, you could still use a common set of nested loops and save a lot of writing.

(2) Other smaller implementation details: some students wrote incorrect boolean expressions for distinguishing between the edge and interior cases; some students mixed up 2-d and 3-d arrays (by assigning one into the other or forgetting the third index); some students didn't know how to use max correctly (covered in the edge-finding example and similar to the neighborhood calculations in Project 4); some students did a lot of extra work (using max and min) to extract reduced-sized neighborhoods but in this question there was no reduced-sized neighborhoods to consider!

Question 6 (2-d array of chars and cell array of strings) :-| :-)

This question was difficult but generally well done! Many students did a great job with extracting a substring of a row in M for comparing with pat:   M(r, c:c+length(pat)-1). Furthermore, most students also recognized that as a result, the loop needed to terminate once c had reached nc-length(pat)+1 (where nc = the number of columns in M) to avoid going out of bounds. A common implementation error was starting at column c+length(pat)+1 instead of column 2--pat did not appear at the beginning of the row, but it could appear anywhere after that, including starting at column 2.

One common algorithmic error that we saw was (automatically) using the nested for-loop pattern for traversing a matrix and then adding another loop to search for pat:

  for r=1:nr
     foundPat= 0;
     for c=1:nc  % <------------- this loop is extraneous!
        while c<=nr-lenth(pat)+1 && foundPat<2 
            %blah blah blah
        end
     end
  end
You need one loop to move row to row (the outer for-loop above) and another to move through the columns as you perform the search column by column (the while-loop above); so you already have the two loops for traversing the 2-d array. A third loop was not necessary and would render the solution incorrect.

Another common error was not stopping the iteration over a row after two instances of pat had been found on that row. Similar to problem 2, such a solution would be highly inefficient, and possibly incorrect if the code continued on to extract more than two substrings from the row to assign to cell array CA. Using a while-loop (e.g., as shown above) would allow the code to stop as soon as possible--right after finding two instances of pat.

Statisics

Max: 100
Median: 81
Mean: 78.3
S.Dev: 14.7
% Given yourScore, action to take after prelim ...

if  yourScore > 87

    toDo= celebrate + later look at the solution

elseif  yourScore > 70

    toDo= redo problems + later check solution

else

    toDo= meet with course staff to go over exam ...
          + stay caught up from now on ...
          + do NOT just read the solution--must redo problems
end