Why Nested For Loop Is not showing me the index values from 1 to N interval ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Rizwan Taj
el 13 de Ag. de 2020
Comentada: Rizwan Taj
el 13 de Ag. de 2020
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 ?
0 comentarios
Respuesta aceptada
Stephen23
el 13 de Ag. de 2020
Editada: Stephen23
el 13 de Ag. de 2020
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).
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!