For loops for printing pattern in matlab
52 次查看(过去 30 天)
显示 更早的评论
I am quite new to matlab and just like in python I want to get familiar with loops in matlab therefore I was trying to print the following pattern.
#####
####
###
##
#
When I am using the disp() function everything is being printed on the same line.
0 个评论
采纳的回答
Siraj
2022-6-30
Hi,
It is my understanding that you know the logic of printing the pattern. We will need 2 “for” loops for this pattern, an outer loop to control the total number of lines and an inner loop to control the number of “#” in one line.
Instead of using the “disp()” function, you can use the “fprintf()” function to print the output in different lines.
Hope it helps!
for i = 1:5
for j = i:5
fprintf('#')
end
fprintf(' ')
end
0 个评论
更多回答(1 个)
KSSV
2022-6-30
str = '#' ;
for i = 5:-1:1
s = repmat(str,1,i) ;
fprintf('%s\n',s) ;
fprintf('\n')
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!