Im lost sum1 help me with the code, im new to this Matlab programming
1 次查看(过去 30 天)
显示 更早的评论
Write a script that requests two positive integers greater than 3 (with error checking). The
numbers represent the x- and y-dimensions of a rectangle. Your program should then display a
rectangle of asterisks (*) with the specified dimensions. The rectangle with dimensions x = 5
and y = 4 would be displayed as :
*****
* *
* *
*****
0 个评论
回答(1 个)
sixwwwwww
2013-12-3
Dear Merapelo, you can do it like this:
width = input('Enter width of rectangle (value should be positive and greater than 3): ');
if ~isnumeric(width)
error('Value for width is not a valid number')
elseif width < 4
error('Width is less than 3')
end
height = input('Enter height of rectangle (value should be positive and greater than 3): ');
if ~isnumeric(height)
error('Value for height is not a valid number')
elseif height < 4
error('Height is less than 3')
end
NumberOfAsteriksInEachDirection = 100;
x = [zeros(1, NumberOfAsteriksInEachDirection), linspace(0, width, NumberOfAsteriksInEachDirection), linspace(0, width, NumberOfAsteriksInEachDirection),...
ones(1, NumberOfAsteriksInEachDirection) * width];
y = [linspace(0, height, NumberOfAsteriksInEachDirection), zeros(1, NumberOfAsteriksInEachDirection), ones(1, NumberOfAsteriksInEachDirection) * height,...
linspace(0, height, NumberOfAsteriksInEachDirection)];
plot(x, y, '*'), xlim([-1 width+1]), ylim([-1 height+1])
I hope it helps. Good luck!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!