Prelim 3 Reivew Problems Sol

Prelim 3 Reivew Problems Sol - CS 100M: Prelim 3 Review...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 100M: Prelim 3 Review Problems (Solutions) 1. AllInterval function B = AllInterval(x,a,b) % x is a length-n vector. % B is assigned the value of 1 (true) if every x-value is in the % interval [a,b]. % B is assigned the value of 0 (false) if at least one of the x-values % is not in the interval [a,b]. % Your implementation should make effective use of a while-loop. 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 7-by-5 bitmap of the digit k (regarding 0 as the "tenth" digit.) % Produce a length 10 cell array D which houses 5-by-7 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 k-th 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
Background image of page 2
3. TotalValue % Refer to the Cost-Inventory 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4. VecHasNegAndPos function B = VecHasNegAndPos(x) % x is a length-n 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.
Background image of page 4
Image of page 5
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).

Page1 / 11

Prelim 3 Reivew Problems Sol - CS 100M: Prelim 3 Review...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online