What is the matter with this nested looped?

4 次查看(过去 30 天)
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