Write a function that gives a tree with one input
조회 수: 6 (최근 30일)
이전 댓글 표시
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

댓글 수: 0
답변 (3개)
Les Beckham
2022년 2월 23일
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
Jan
2022년 2월 23일
편집: Jan
2022년 2월 23일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!