フィルターのクリア

For loops for printing pattern in matlab

19 ビュー (過去 30 日間)
Zaid
Zaid 2022 年 6 月 30 日
回答済み: Siraj 2022 年 6 月 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.

採用された回答

Siraj
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.
You can refer to the documentation for loop and fprintf for further understanding.
Hope it helps!
for i = 1:5
for j = i:5
fprintf('#')
end
fprintf(' ')
end
#####
####
###
##
#

その他の回答 (1 件)

KSSV
KSSV 2022 年 6 月 30 日
str = '#' ;
for i = 5:-1:1
s = repmat(str,1,i) ;
fprintf('%s\n',s) ;
fprintf('\n')
end
##### #### ### ## #

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by