This preview shows pages 1–5. Sign up to view the full content.
CS 100M: Prelim 3 Review Problems
(Solutions)
1. AllInterval
function
B = AllInterval(x,a,b)
% x is a lengthn vector.
% B is assigned the value of 1 (true) if every xvalue is in the
%
interval [a,b].
% B is assigned the value of 0 (false) if at least one of the xvalues
%
is not in the interval [a,b].
% Your implementation should make effective use of a whileloop.
Solution:
n = length(x);
B = (a <= x(1)) & (x(1)
k=1
;
% B is true if x(1),.
..,x(k) are in the interval.
..
k = k+1;
% B "turns false" if x(k) not in [a,b].
..
end
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 2. Transpose
% Refer to lecture 18 and the function TheDigits:
C = TheDigits;
% C is a length 10 cell array with the property that C{k} is
% a 7by5 bitmap of the digit k (regarding 0 as the "tenth" digit.)
% Produce a length 10 cell array D which houses 5by7 bitmaps
% of the "transposed" arrays. Thus, since
%
% C{1} looks like
%
00100
%
01100
%
00100
%
00100
%
00100
%
00100
%
01110
% then D{1} should look like
%
%
0000000
%
0100001
%
1111111
%
0000001
%
0000000
Solution:
D = cell(10,1);
for k=1:10
% Produce the kth transposed bit map
M = C{k};
% The transpose will be stored in MT.
..
MT = zeros(5,7);
% Examples: M(1,3) > MT(3,1), M(7,5 )> MT(5,7) etc
for i=1:5
for j=1:7
MT(i,j) = M(j,i);
end
end
D{k} = MT;
end
2
3. TotalValue
% Refer to the CostInventory application in Lecture 17
% Complete the following function.
..
function T = TotalValue(Inv,Cost)
% T is the total value of all the inventory in all the factories
Solution:
[nFact,nProd] = size(Inv);
T=0
;
for i=1:nFact
% Compute the value of factory i’s inventory
S=0;
for j=1:nProd
S = S + Inv(i,j)*Cost(i,j);
end
T=T+S;
end
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 4. VecHasNegAndPos
function
B = VecHasNegAndPos(x)
% x is a lengthn vector and n>=2
% B is assigned the value of 1 (true) if x has at least one component
%
that is strictly negative and at least one component that is
%
strictly positive.
% Otherise B should be assigned the value of 0.
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 02/28/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).
 Fall '07
 FAN/VANLOAN

Click to edit the document details