Why Nested For Loop Is not showing me the index values from 1 to N interval ?

1 次查看(过去 30 天)
I know its a silly question and i am not new to programming language. I have tried doing debugging so far the things i know about Matlab. I am running a script but here i will share the demo version of a code snippet that is bothering me. I am running this code
clc;clear;close all;
for i=1:100
for j=1:60
k = i;
disp(num2str(k))
end
end
As per i know or may be i am not familiar with Matlab it should start from 1-100 but the output is showing from 9-100. Why its not starting from 1 ?
9
9
9
9
9
9
9
9
9
9
9 ... continous till 100
How this nested loop is working ?

采纳的回答

Stephen23
Stephen23 2020-8-13
编辑:Stephen23 2020-8-13
MATLAB does not display infinite lines in the command window scroll buffer. By default it displays the last 5000 lines, as explained here:
You printed
>> 100*60
ans = 6000
lines to the command window and by default only the last 5000 are displayed, which means those lines with 9 will be shown when you scroll up to the very top of the command window.
"Why Nested For Loop Is not showing me the index values from 1 to N interval ?"
It does: those values are printed to the command window. And then they scroll up (and eventually out of the display scroll buffer) when you print more lines at the bottom.
"How this nested loop is working ?"
Just like you expect it to.
Of course the display scroll buffer has nothing to do with nested loops, you would get the same effect using just one loop iterating over 1:6000, or anything else that prints more than 5000 lines (or whatever limit your scroll buffer is set to).

更多回答(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