270 likes | 442 Views
fprintf and other examples. fprint function. >> a=2;b=3;c=5; >> fprintf('a = %d , b = %d, c = %d', a, b, c); a = 2 , b = 3, c = 5 >> fprintf('a=%d , b=%d', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3a+b=5 >> fprintf('a=%d , b=%d <br>', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3 a+b=5. fprintf.
E N D
fprint function >> a=2;b=3;c=5; >> fprintf('a = %d , b = %d, c = %d', a, b, c); a = 2 , b = 3, c = 5 >> fprintf('a=%d , b=%d', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3a+b=5 >> fprintf('a=%d , b=%d \n', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3 a+b=5
fprintf >> a = 3.7; >> fprintf('a=%d , b=%d \n', a, b);fprintf('a+b=%d ', a+b); a=3.700000e+000 , b=3 a+b=6.700000e+000 >> fprintf('a=%f , b=%d \n', a, b);fprintf('a+b=%d ', a+b); a=3.700000 , b=3 a+b=6.700000e+000
Drawing a Rectangle Block >> drawRectangleBlock (3, 4, 7, 4) ####### ####### ####### #######
Algorithm Given x, y, width, height : go down y-1 lines for the next height lines do put x-1 space signs print width ‘#’ characters
Matlab program function drawRectangleBlock(x, y, width, height) for i=1:y-1 fprintf('\n'); end for i = y : y + height - 1 for j = 1 : x-1 fprintf(' '); end for j = x : x + width - 1 fprintf('#'); end fprintf('\n'); end
Draw a Pyramid >> drawPyramid(3, 5, 5) # ### ##### ####### #########
Algorithm Given x, y, height : go down y-1 lines for line 1 to height do put x - line space signs print 2 * line - 1 ‘#’ characters
Matlab Program function drawPyramid(x, y, height) for i=1:y-1 fprintf('\n'); end for line = 1:height for j = 1 : x - line fprintf(' '); end for j = 1 : 2 * line - 1 fprintf('#'); end fprintf('\n'); end
Another Way function drawPyramid(x, y, height) for i=1:y-1 fprintf('\n'); end noPieces = 1; noSpaces = x - 1; for line = 1:height for j = 1 : noSpaces fprintf(' '); end for j = 1 : noPieces fprintf('#'); end fprintf('\n'); noSpaces = noSpaces - 1; noPieces = noPieces + 2; end
banner problem • design a program that is much like unix command banner: • each character is a 8x8 combination of ‘#’
Drawing Rectangles • Given a matrix where each row has the parameters of one rectangle, draw all the rectangles on screen • A matrix with n rectangles looks like : [ x1 y1 width1 height1 x2 y2 width2 hegiht2 … xn yn widthn heightn]
Example For m = 3 5 5 8 17 1 7 31 12 8 17 8 1 20 32 6
Drawing Pyramids • Given a matrix where each row has the parameters of one pyramid, draw all the pyramids on screen • A matrix with n pyramids looks like : [ x1 y1 height1 x2 y2 hegiht2 … xn yn heightn]
Pyramid Example p = 5 5 4 11 3 10 20 7 9
Pyramids Design • For j = 1 to max y location do • for i = 1 to max x location do • isFull = false • for all pyramids p do • if p contains point (i, j) • isFull = true • if (isFull) • print character ‘#’ • else • print character ‘ ‘ • move to next line
Rectangles Design ? • the logic is very similar, except we ask whether current rectangle contains (i, j) we could have : • for all shapes s do • if s is a pyramid • if pyramid s contains (i, j) … • else if s is a rectangle • if rectangle s contains (i, j) … • …
Max x location • Given a matrix m of pyramids or rectangles • For rectangles the max x value of a rectangle is x + width – 1 • Create a vector of x + width -1 return the max • max( m(:, 1) + m(:, 3) – 1) • For pyramids: x + height – 2 • Create a vector of x + height – 2, return max • max( m(:, 1) + m(:, 3) – 2)
Max y location • Given a matrix m of pyramids or rectangles • For rectangles the max y value of a rectangle is y + height – 1 • Create a vector of y + height -1 return the max • max( m(:, 2) + m(:, 4) – 1) • For pyramids: y + height – 1 • Create a vector of y + height – 1, return max • max( m(:, 2) + m(:, 3) – 1)
Max Location finding • In all cases, given a matrix, we add up certain columns and find the maximum value of the addition • Let’s implement a function to do that
findMaxLocation Given a matrix m and wo columns c1 & c2: function maxLocation = findMaxLocation(m, c1, c2) vals = m(:, c1); deltas = m(:, c2); totals = vals + deltas - 1; maxLocation = max(totals);
Test findmaxlocation function p = 5 5 4 11 3 10 20 7 9 >> findMaxLocation(p, 1, 3) ans = 28 >> findMaxLocation(p, 1, 2) ans = 26
function isInRectangle function isinside = isInRectangle(i, j, x, y, width, height) if (x <= i && i < x + width) && ... (y <= j && j < y + height) isinside = true; else isinside = false; end
function isInRectangle • Since our rectangles are stored in vectors: function isinside = isInRectangle2(i, j, rect) isinside = isInRectangle(i, j, rect(1), rect(2), rect(3), rect(4));
function isinside = isInPyramid(i, j, x, y, height) % First check the bounding rectangle sx = x - height + 1; sy = y; width = 2 * height - 1; if ~(isInRectangle(i, j, sx, sy, width, height)) isinside = false; return; end lineno = j - sy + 1; nospaces = height - lineno; nopieces = 2 * lineno - 1; if ( i < sx + nospaces || i >= sx + nospaces + nopieces) isinside = false; else isinside = true; end
function printPyramids(pyramids, ch) maxx = findMaxLocation(pyramids, 1, 3); maxy = findMaxLocation(pyramids, 2, 3); for j = 1 : maxy for i = 1 : maxx isFilled = false; for pno = 1 : size(pyramids, 1) pyr = pyramids(pno, :); if (isInPyramid2(i, j, pyr)) isFilled = true; break; end end if (isFilled) fprintf(ch); else fprintf(' '); end end fprintf('\n'); end