How to use nested for loops?

조회 수: 4 (최근 30일)
Yasmin Touly
Yasmin Touly 2018년 4월 10일
댓글: Birdman 2018년 4월 10일
I am very new to MATLAB, and I have been trying to figure out how to use nested for loops correctly. I want to print out :
1
121
12321
1234321
123454321
what should I do if there are a varying number of columns? Any help would be greatly appreciated.
  댓글 수: 3
Yasmin Touly
Yasmin Touly 2018년 4월 10일
@Walter Roberson, since it is not an array, I am not sure what exactly I should do. I am lost.
Walter Roberson
Walter Roberson 2018년 4월 10일
for variable = something : something_else
for another_variable = thing2 : thing3
% do something involving variable and another_variable
end
end

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

답변 (1개)

Birdman
Birdman 2018년 4월 10일
편집: Birdman 2018년 4월 10일
No need for nested for loops. Try this:
i=1;n=5;
while i<=n
fprintf('%d',[1:i-1 i:-1:1]);
fprintf('\n');
i=i+1;
end
  댓글 수: 6
Yasmin Touly
Yasmin Touly 2018년 4월 10일
@Birdman, it is, but I wanted to know how I can specifically use nested for loops for this particular question.
Birdman
Birdman 2018년 4월 10일
n=5;
for j=1:n
for i=1:j
end
fprintf('%d',[1:i-1 i:-1:1]);
fprintf('\n');
end

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

카테고리

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