fprintf and other examples
DESCRIPTION
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 \n', a, b);fprintf('a+b=%d ', a+b); a=2 , b=3 a+b=5. fprintf. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/1.jpg)
fprintf and other examples
![Page 2: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/2.jpg)
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
![Page 3: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/3.jpg)
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
![Page 4: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/4.jpg)
Drawing a Rectangle Block
>> drawRectangleBlock (3, 4, 7, 4)
####### ####### ####### #######
![Page 5: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/5.jpg)
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
![Page 6: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/6.jpg)
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
![Page 7: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/7.jpg)
Draw a Pyramid
>> drawPyramid(3, 5, 5)
# ### ##### ################
![Page 8: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/8.jpg)
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
![Page 9: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/9.jpg)
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
![Page 10: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/10.jpg)
Another Wayfunction 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
![Page 11: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/11.jpg)
banner problem
• design a program that is much like unix command banner:
• each character is a 8x8 combination of ‘#’
![Page 12: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/12.jpg)
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]
![Page 13: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/13.jpg)
Example
For m =
3 5 5 8 17 1 7 31 12 8 17 8 1 20 32 6
![Page 14: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/14.jpg)
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]
![Page 15: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/15.jpg)
Pyramid Example
p =
5 5 4 11 3 10 20 7 9
![Page 16: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/16.jpg)
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
![Page 17: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/17.jpg)
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) …
– …
![Page 18: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/18.jpg)
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)
![Page 19: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/19.jpg)
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)
![Page 20: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/20.jpg)
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
![Page 21: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/21.jpg)
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);
![Page 22: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/22.jpg)
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
![Page 23: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/23.jpg)
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
![Page 24: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/24.jpg)
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));
![Page 25: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/25.jpg)
function isinside = isInPyramid(i, j, x, y, height)
% First check the bounding rectanglesx = 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
![Page 26: fprintf and other examples](https://reader035.vdocuments.mx/reader035/viewer/2022062308/56812bc6550346895d9011a6/html5/thumbnails/26.jpg)
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