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 :
*****
* *
* *
*****

回答(1 个)

sixwwwwww
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!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by