Decrementing for loop in MATLAB

287 visualizaciones (últimos 30 días)
Jatin Arora
Jatin Arora el 29 de Nov. de 2012
Editada: Dennis M el 24 de Ag. de 2021
Hello everyone,
I am new to MATLAB programming and I want to use a for loop starting with an index 5 and reducing to 1
Example in C++ I can write for int i = 5;i<=1;i--)
how can this be implemented in MATLAB

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Nov. de 2012
for k=5:-1:1
%do
end
  1 comentario
Dennis M
Dennis M el 24 de Ag. de 2021
Editada: Dennis M el 24 de Ag. de 2021
y = zeros(1,12);
for i = 1:12
y(i+1) = y(i) + ((5 - (4/50)*y(i)));
end
y
x = zeros(1,10);
for i = 10:-1:2
x(i-1) = x(i) + ((5 + (4/50)*x(i)));
end
x
Good Day,
May I ask how can I insert the start and stop variable on above code? for example I want to start in 17 to 30 or 15 to 2 with decrementing step.
Thanks and Regards,
Dennis

Iniciar sesión para comentar.

Más respuestas (2)

yagnesh
yagnesh el 29 de Nov. de 2012
for i= 5:-1:1 %here we have used reverse for loop to overcome with error statement end
  1 comentario
dhrumil sheth
dhrumil sheth el 25 de Mayo de 2016
Very Good. It is really helpful for me

Iniciar sesión para comentar.


Jan
Jan el 29 de Nov. de 2012
Editada: Jan el 29 de Nov. de 2012
It is always a good idea to read the documentation in case of questions:
There you find a corresponding example.

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by