CS 221 PRACTICE Lab Quiz 2 2011

Part 1: MATLAB Programming

Ben G. Near works for a utility, where his job is to collect a large number of meter readings (taken by other people periodically), and use MATLAB to analyze them to identify possible issues with the equipment. Unfortunately, with so many meters to read, people often make mistakes reading or entering the data.

Ben tried to write a MATLAB script to check the data for entry errors or possibly broken meters. The idea is simple: The data is in the form of a table. The first column is the meter number (a positive integer). The other columns are the actual meter readings, from oldest to newest. If the readings are correct, the numbers should increase as you go through the array.

The script is supposed to look at each row in the table and compare the two most recent meter readings (the ones in the last two columns)—and only those two. If the most recent number is smaller than the previous reading, someone probably read or entered it incorrectly. If, on the other hand, the reading did not change, then the meter may be broken. Either way, the script should produce an indication of the possible problem, so someone can be sent out to check the meter. The output should look like this (including the precision displayed for floating-point numbers):

>> run metercheck
Check meter no. 1486 (went from  7432.27 back to   681.89)
Check meter no. 1656 (went from  8496.63 back to  7965.04)
Meter no. 1241 remained at  4019.79.
The script does not print anything for meters that appear to be reading correctly.

Unfortunately, Ben has not taken CS 221, and the code he wrote contains several errors. Please help Ben by correcting the code to make it work as expected (i.e., as described above).

Part 2: MATLAB Functions

Write a MATLAB function selectsum() that takes two 2-D arrays as arguments, which are assumed to have the same dimensions. The first argument, A, is an array of boolean values (i.e., every entry is either one or zero). The second argument, B, is an array of numbers. The function should sum up all the entries of B where A has a true (nonzero) value, and return the total sum over all those entries of B. In other words, an element B(row,column) is included in the sum only if A(row,column) is nonzero.

Here is an example. If A and B are the following arrays:

A = [ 0 1 0 1; 1 0 1 0; 0 0 0 1 ];

B = [ 10 20 30 40; 5  10 15 20; 1  2  3  4 ];
Then selectsum(A,B) should return 84 (= 20 + 40 + 5 + 15 + 4).

Here is another example:

A = [ 0, 0, 0, 0, 1;
      1, 0, 1, 1, 0 ];

B = [ 0.4, 4.4, 1.9, 10.2, 0;
      0, 1, 0, 0, 100]
For the above values of A and B, selectsum(A,B) should return 0.

The skeleton of the code is provided for you here.