Write a function that gives a tree with one input

5 vues (au cours des 30 derniers jours)
Joe Ronald
Joe Ronald le 23 Fév 2022
Commenté : Jan le 24 Fév 2022
Hello,
I have no idea of how to do something like that i tried to sum but you can't do so with string thanks

Réponses (3)

Les Beckham
Les Beckham le 23 Fév 2022
n = 9;
tree(9);
* *** ***** ******* ********* *********** ************* *************** *****************
function tree(n)
for i = 1:n
fprintf('%s', repmat(' ', 1, n-i));
fprintf('%s\n', repmat('*', 1, 1 + 2*(i-1)))
end
end
  2 commentaires
Walter Roberson
Walter Roberson le 23 Fév 2022
Note that this is a homework question
Les Beckham
Les Beckham le 23 Fév 2022
mea culpa
I got carried away.

Connectez-vous pour commenter.


Jan
Jan le 23 Fév 2022
Modifié(e) : Jan le 23 Fév 2022
n = 9;
disp(char(10 * (abs(-n+1:n-1) < (1:n).') + ' '))
* *** ***** ******* ********* *********** ************* *************** *****************
Or with a function:
affAbre(n)
function affAbre(n)
s = repmat(' ', 1, 2*n-1);
for k = 0:n-1
s(n-k:n+k) = '*';
disp(s)
end
end
Come on. Be a little bit creative. Try to understand the given solutions and invent a new one.
Of course, I assume that this is a homework question and posting solutions does not help you. I hop, the different approachs motivate you to learn Matlab.
  2 commentaires
Joe Ronald
Joe Ronald le 23 Fév 2022
Well your right but i was just stuck tried every way i could think about :/
Thanks
Jan
Jan le 24 Fév 2022
Are you unstuck now?
Did you understand, how my first suggestion is working?

Connectez-vous pour commenter.


Joe Ronald
Joe Ronald le 23 Fév 2022
thanks for the answers

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by