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

6 visualizaciones (últimos 30 días)
cha hong
cha hong el 30 de Mayo de 2017
Comentada: Rik el 11 de Mzo. de 2021
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 comentarios
Rik
Rik el 30 de Mayo de 2017
Have a read here. It will greatly improve your chances of getting an answer.
Jan
Jan el 30 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (3)

Andrei Bobrov
Andrei Bobrov el 30 de Mayo de 2017
Editada: Andrei Bobrov el 30 de Mayo de 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 el 6 de Oct. de 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 el 26 de Mzo. de 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 comentarios
Vinit Ghone
Vinit Ghone el 11 de Mzo. de 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 el 11 de Mzo. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre 루프와 조건문 en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!