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

6 ビュー (過去 30 日間)
cha hong
cha hong 2017 年 5 月 30 日
コメント済み: Rik 2021 年 3 月 11 日
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 件のコメント
Rik
Rik 2017 年 5 月 30 日
Have a read here. It will greatly improve your chances of getting an answer.
Jan
Jan 2017 年 5 月 30 日
@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.

サインインしてコメントする。

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 30 日
編集済み: Andrei Bobrov 2017 年 5 月 30 日
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 2019 年 10 月 6 日
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 2020 年 3 月 26 日
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 件のコメント
Vinit Ghone
Vinit Ghone 2021 年 3 月 11 日
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 2021 年 3 月 11 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange루프와 조건문 についてさらに検索

Community Treasure Hunt

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

Start Hunting!