how can I write a rectangle and a square which sizes are users chooses numbers? does it must be with matrix or whatever else?
what ı mean ıs down there

 Accepted Answer

Jan
Jan on 24 Mar 2021
S = repmat('*', n, n) % A square
% Or:
S = '';
S(1:n, 1:n) = '*';
For a square, you have to set the elements (2:n-1, 2:n-1) to the space afterwards.

4 Comments

ıt works, thank you so much! But I couldnt find how to print an empty square
See this:
S = repmat('*', n, n);
S(2:3, 3:4) = '#'
this code doesnt work for an empty square
Jan
Jan on 24 Mar 2021
Which code does not run for what input? What exactly is "an empty square"? n=0?
What does "doesn't work" mean? Please post the code you use and the error message you get.
I showed how to insert # characters as a rectangular shape. I assume, that you can replace '#' by ' ' and 2:3 by 2:n-1 .

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Mar 2021

Commented:

Jan
on 24 Mar 2021

Community Treasure Hunt

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

Start Hunting!