How can print out the below content by MATLAB program?

조회 수: 1 (최근 30일)
Chao-Zhen Liu
Chao-Zhen Liu 2019년 4월 19일
댓글: Star Strider 2019년 4월 19일
Hi,
i want to use the MATLAB to print out the below result, but I could not put the values in each row, how can I revise my program?
Thanks!
What I want to print out:
1
21
321
4321
54321
And there are my code & result:
for i = 1:1:5
for j =i:-1:1
disp(j);
end
end
1
2
1
3
2
1
4
3
2
1
5
4
3
2
1

채택된 답변

Star Strider
Star Strider 2019년 4월 19일
You do not need the second for loop. Simply print the value of ‘j’ in each iteration of the ‘i’ loop:
for i = 1:1:5
j =i:-1:1;
disp(j)
end
producing:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
  댓글 수: 2
Chao-Zhen Liu
Chao-Zhen Liu 2019년 4월 19일
Hi Star,
Thanks for your solution!
Star Strider
Star Strider 2019년 4월 19일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by