How to write a program to draw a diamond of stars in MATLAB?

6 views (last 30 days)
I don't know use (for, if, while. how) to write a program to draw a diamond of stars.
I bought the book and tried to solve the example, but I could not find a solution.
I want to use these at once to create code.
The problem is this 'Create using [For, if, while]'
*
***
*****
*******
*****
***
*
  2 Comments
Rik
Rik on 30 May 2017
Have a read here. It will greatly improve your chances of getting an answer.
Jan
Jan on 30 May 2017
@cha hong: Please ask a specific question. It would not be useful, if we post a solution here - most of all because we do not know the actual question.
Show us, what you have tried so far and ask a specific question.

Sign in to comment.

Answers (3)

Andrei Bobrov
Andrei Bobrov on 30 May 2017
Edited: Andrei Bobrov on 30 May 2017
n = 7;
m = ceil(n/2);
ii = toeplitz([ones(m,1);zeros(n-m,1)]);
out = repmat(' ',n);
out(ii & rot90(ii)) = '*'

Rahul Patel
Rahul Patel on 6 Oct 2019
for i=1:2:9
for l=9:-2:i
fprintf(' ')
end
for j=1:i
fprintf('*')
end
fprintf('\n')
end
for i=9:-2:1
for l=i:2:9
fprintf(' ')
end
for j=i:-1:1
fprintf('*')
end
fprintf('\n')
end

Md. Hasibur Rahman Niloy
Md. Hasibur Rahman Niloy on 26 Mar 2020
for r=1:a
%space
for j=a:-1:r
fprintf(' ');
end
%pattern
for j=1:r
fprintf(' %d',j);
end
fprintf('\n');
end
for r=1:a
for j=1:r
fprintf(' ');
end
for j=a:-1:r
fprintf(' %d',r);
end
fprintf('\n');
end
  5 Comments
Vinit Ghone
Vinit Ghone on 11 Mar 2021
Yes, I did, didn't understood it as I am just started to learn MATLAB to develop logic. Still could you please explain?
Rik
Rik on 11 Mar 2021
fprintf will print the FormatSpec, replacing special tokens. That means '\n' will be replaced by a newline character etc. 'a\n' will therefore print the letter a and a newline. The percent symbol will make fprintf look at the further input you have provided.

Sign in to comment.

Categories

Find more on 루프와 조건문 in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!