What is the matter with this nested looped?

조회 수: 4 (최근 30일)
Nguyen Vinh
Nguyen Vinh 2021년 6월 5일
답변: Scott MacKenzie 2021년 6월 5일
The below is my code, but it can product the same result as the above.
n = input('Enter a number: ');
A = zeros(n);
for x = n:1
for y = 1:n
A(n-(n-x),n-(n-y)) = 1;
end
end
  댓글 수: 3
Atsushi Ueno
Atsushi Ueno 2021년 6월 5일
A(n-(n-x),n-(n-y)) = 1;
n-(n-x) is same as x, and n-(n-y) is same as y. What is your intention?
Atsushi Ueno
Atsushi Ueno 2021년 6월 5일
To debug your code, how about removing semi-colon to display the result of A for each iteration?
A(n-(n-x),n-(n-y)) = 1

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

답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 5일
Most likely the problem is with this line
for x=n:1
if n > 1, the loop does not execute. Did you mean
for x=1:n

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by