Anda di halaman 1dari 6

Spring 2014 Prelim 2 Solutions

Question 1: (15 points)


(a) What is the output from executing the following fragment? Write the word error instead of the
output if executing the fragment would cause a run-time error.
M = [2 1 6 8; ...
5 2 1 2; ...
7 9 2 6; ...
3 0 4 1];
A = zeros(4,4);
for r= 1:4
for c= 1:r-1
A(r,c)= M(r,c);
end
end
disp(A)
Solution:
0
5
7
3

0
0
9
0

0
0
0
4

0
0
0
0

(b) Complete the function as specified.


function drawHistogram(yr, maj, greek)
% Draw a histogram showing the number of undergraduate students who participate
% in Greek life (in a fraternity or sorority) in each of the 75 majors at Cornell.
% yr, maj, and greek are vectors of the same length. For each valid index k,
%
yr(k) is the year code of the kth student. Possible values are integers in
%
[1..13]; values 1,2,3,4 indicate undergraduate.
%
maj(k) is the major code of the kth student; possible values are integers
%
in [1..75].
%
greek(k) is 1 if the kth student participates in Greek life; 0 otherwise.
data= zeros(1,75);
% data(i) will store the number of undergrad students in major i who
% participate in Greek life, i=1,2,...,75.
for k= 1:length(yr)
if

yr(k)<=4

&&

greek(k)

binNum= maj(k);
data(binNum) = data(binNum) + 1;
end
end
bar(1:75, data)
title(Number of undergrads in each major who participate in Greek life)
xlabel(Major code);
ylabel(Count)

Spring 2014 Prelim 2 Solutions

Question 2: (15 points)


(a) Circle all the statements below that assign to a variable the string smart.
THIS ONE-->

a = [s m a r t]

THIS ONE-->

b = smart
c = {s mart}

THIS ONE-->

d = [c{1} c{2}]
e = {c{1} c{2}}
f = {b }

(b) The script below reads a plain text file. Add code to the script to determine and print the number
of lines in the file that begin with the string DATA. Assume that the text file myData.txt is accessible
and not empty. Do not modify or cross out the given statements and do not use built-in functions find
or strfind.
% Read a text file
fid = fopen(myData.txt,r);

n = 0;

%%%% Initialize a counter anywhere above while

while ~feof(fid)

s = fgetl(fid);
if

length(s)>=4 && strcmp(s(1:4),DATA) %%%% Must check length>=4.


%% Just checking ~isempty is
n = n + 1;
%% not enough.
end
%%%%

end

disp(n)

%%%% Can print anywhere after while loop

fclose(fid);

Spring 2014 Prelim 2 Solutions




Question 3: (25 points)


(a) Complete the following function as specified:
function Q = arrangeBlocks(P, m)
% P is a 3-d uint8 image array. P consists of a*b blocks arranged in a
%
grid; a and b are the respective number of rows and columns of blocks
%
in the grid. All the blocks have the same size.
% Q is a 3-d uint8 image array in which all a*b blocks of P have been
%
rearranged to form a single vertical stack. The position in which the
%
blocks appear in the stack is described by the matrix m. Stack positions
%
are indexed from the top to bottom, with the top position corresponding to
%
index 1 and the bottom position corresponding to index a*b.
% m is an a-by-b matrix of type double. Each entry in m stores a unique value
%
from 1 to a*b. The value in m(i,j) gives the position at which the (i,j)th
%
block of P should appear in Q.
% ONLY these functions are allowed: size, ones, uint8.
% Example:
%
m is [
%
%

In the diagrams above, P is made up of 3*2=6 blocks. If the matrix


3 5 ; ..
1 6 ; ..
4 2 ]
then the 6 blocks will be re-arranged in Q as shown above.

[a,b] = size(m);
[nrp, ncp, nlp] = size(P);

% nrp is a multiple of a; ncp is a multiple of b

Example solution:
nr= nrp/a;
nc= ncp/b;

% number of rows of pixels in each block of P


% number of columns of pixels in each block of P

for i= 1:a
for j= 1:b
% block ij
blk= P( (i-1)*nr+1 : i*nr,

(j-1)*nc+1 : j*nc ,

ind= m(i,j);
Q( (ind-1)*nr+1 : ind*nr , 1:nc , : )= blk;
end
end

Part (b) appears on the next page.


4

: );

3(b) Implement the following function as specified

Spring 2014 Prelim 2 Solutions

function g = darkerGray(Z, a, b, i, j)
% Z is a 3-d uint8 image array.
% Calculate the gray values of pixels (a,b) and (i,j) of array Z and assign
% to g the darker of the two gray values. g is a type uint8 scalar.
% Calculate the gray value as the arithmetic mean of the red, green, and
% blue values. Assume that a, b, i, and j are valid indices in array Z.
% ONLY these functions are allowed (although you may not need them): uint8, double.
Example solution:
gab= Z(a,b,1)/3 + Z(a,b,2)/3 + Z(a,b,3)/3;
gij= Z(i,j,1)/3 + Z(i,j,2)/3 + Z(i,j,3)/3;
if gab < gij
g= gab;
else
g= gij;
end

The space below is intentionally left blank.

Spring 2014 Prelim 2 Solutions

Question 4: (20 points)


A student organization needs to pick three of its students to form one relay team for a charity race.
The organizations data is stored in a 1-dimensional array S of structures. Each struct stores the data
of one student and has these fields:
name: the name of the student, a string
col: the college in which the student is enrolled, a string (e.g., ENG, CALS, etc.)
sem: the number of semesters the student has completed, an integer
There is a rule for the charity race: the three members of a relay team must represent at least two
colleges and together have completed 10 semesters.
Assume that S, the 1-d array of student structs, is given. Write a code fragment below to determine
all possible, unique, 3-member teams that can be formed according to the rule of the race. Store the
result in an n-by-3 array T of student structs such that n is the number of possible teams (which you
have to determine) and each row of T stores the three structs of the members of one possible team. If
no team can be formed according to the rule of the race, make T an empty array of any type. Your
code should be efficientavoid unnecessary iterations.
% Assume 1-d struct array S is given, is as described above, and has a length >3.
% Write your code below to create the appropriate 2-d struct array T.
n= 0;

% number of possible teams so far

nPeople= length(S);
for i = 1 : nPeople-2
% going all the way to n is OK
for j = i+1 : nPeople-1 % going all the way to n is OK
for k = j+1 : nPeople
if (~strcmp(S(i).col, S(j).col) || ...
~strcmp(S(j).col, S(k).col) || ...
~strcmp(S(i).col, S(k).col) ) && ...
S(i).sem + S(j).sem + S(k).sem == 10
n = n + 1;
T(n,1)= S(i);
T(n,2)= S(j);
T(n,3)= S(k);
end
end
end
end
if n==0
T= [];
end

Spring 2014 Prelim 2 Solutions

Question 5: (25 points)


Implement the following function as specified and note the example at the bottom of the page:
function Z = getDNAStrands(M, p)
% M: a 2-d simple array of characters. Each row represents a DNA strand.
% p: a 1-d simple array of characters (a string). The length of p is
%
smaller than the number of columns in M.
% Z: a 1-d cell array of partial DNA strands (described below), or an empty
%
cell array if no partial strands are found.
% For each row (strand) in M, determine whether the DNA pattern p appears at
% least three times. If so, store that strand, up to and including the third
% occurrence of the pattern, in one cell of Z.
% ONLY these functions are allowed: strcmp, size, length, cell

[nr, nc]= size(M);


np= length(p);
k= 0;

% number of strands so far

for r= 1:nr
% Check the string in M(r,:)
nFound= 0; % number of matches with p found so far
c= 1;
while c<=nc-np+1 && nFound<3 % more efficient to use while than for
if strcmp(p, M(r,c:c+np-1))
nFound= nFound + 1;
end
c= c+1;
end
if nFound==3
k= k+1;
Z{k}= M(r, 1:(c-1)+np-1);
end
end
if k==0
Z= {};
end

Suppose M is the following matrix:


M = [acggtttgtgtaa; ...
ggtgtaaaaaaaa; ...
gtgtcgtcccgtt]
Then the function call getDNAStrands(M, gt) should return a cell array with two strings:
acggtttgtgt and gtgtcgt.
The function call getDNAStrands(M, aaa) should return a cell array with one string: ggtgtaaaaa.

Anda mungkin juga menyukai