Convert the for loop into while loop.

5 visualizaciones (últimos 30 días)
Biswajit  Jana
Biswajit Jana el 4 de Jul. de 2018
Editada: Jan el 4 de Jul. de 2018
matrix=[6 3 5; 8 4 6; 2 1 9;7 5 2]
[r,c]=size(matrix);
for Row=1:r
for Col=1:c
fprintf('Element(%d,%d)=%d.\n',Row,Col,matrix(Row,Col))
end
end
  4 comentarios
Jan
Jan el 4 de Jul. de 2018
Editada: Jan el 4 de Jul. de 2018
I hesitate to convert your code directly to a running version with while, because this might be a homework question.
Biswajit  Jana
Biswajit Jana el 4 de Jul. de 2018
Nope not a homework, just trying to do same code in different loops and all,kindly help me out with while.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 4 de Jul. de 2018
for i = 1:10
disp(i)
end
is equivalent to:
i = 1;
while i <= 10
disp(i)
i = i + 1;
end
  3 comentarios
Rik
Rik el 4 de Jul. de 2018
This concept should be easy to apply to your case. Why don't you try something and show the result here if it fails?
Jan
Jan el 4 de Jul. de 2018
Editada: Jan el 4 de Jul. de 2018
@Biswajit Jana: You know how to convert one for loop into a while loop. Your code has two for loops. You can simply copy my example and paste it twice inside each other. Then rename the loop counters and limits. It is not hard. Please try it.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by