1 / 26

fprintf and other examples

fprintf and other examples. fprint function. &gt;&gt; a=2;b=3;c=5; &gt;&gt; fprintf('a = %d , b = %d, c = %d', a, b, c); a = 2 , b = 3, c = 5 &gt;&gt; fprintf('a=%d , b=%d', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3a+b=5 &gt;&gt; fprintf('a=%d , b=%d <br>', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3 a+b=5. fprintf.

Download Presentation

fprintf and other examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. fprintf and other examples

  2. 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

  3. 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

  4. Drawing a Rectangle Block >> drawRectangleBlock (3, 4, 7, 4) ####### ####### ####### #######

  5. 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

  6. 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

  7. Draw a Pyramid >> drawPyramid(3, 5, 5) # ### ##### ####### #########

  8. 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

  9. 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

  10. 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

  11. banner problem • design a program that is much like unix command banner: • each character is a 8x8 combination of ‘#’

  12. 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]

  13. Example For m = 3 5 5 8 17 1 7 31 12 8 17 8 1 20 32 6

  14. 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]

  15. Pyramid Example p = 5 5 4 11 3 10 20 7 9

  16. 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

  17. 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) … • …

  18. 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)

  19. 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)

  20. 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

  21. 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);

  22. 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

  23. 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

  24. 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));

  25. 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

  26. 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

More Related