nested for loop keeping i constant through all, until end of j's

Hi, I am running a nested for loop
for i
for j
end
end
and I would like i=1 while j=1, 2, 3, 4, 5
i=2 j=1, 2, 3, 4, 5
i=3 j=1, 2, 3, 4, 5
...
i=5 j=1, 2, 3, 4, 5
is this an if or a while loop?
Thanks for any help!

2 comentarios

Stephen23
Stephen23 el 17 de Nov. de 2015
Editada: Stephen23 el 17 de Nov. de 2015
Note that you should not name your variables i or j, as these are both names of the inbuilt imaginary unit. Common alternatives are ii and jj, or more descriptive names such as iter_row.
Thorsten
Thorsten el 17 de Nov. de 2015
Editada: Thorsten el 17 de Nov. de 2015
i and j are very common loop counters, and they result in code that is readable and concise. I think it's ok to use them and use 1i for the complex unit, as advised by Matlab's help page:
For speed and improved robustness in complex arithmetic, use 1i and 1j instead of i and j.

Iniciar sesión para comentar.

Respuestas (1)

Thorsten
Thorsten el 17 de Nov. de 2015
Editada: Thorsten el 17 de Nov. de 2015
Use two for-loops:
for i=1:4
fprintf('i = %d, j = ', i)
for j = 1:4
fprintf('%d,', j)
end
fprintf('\b\n')
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Nov. de 2015

Editada:

el 17 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by