Decrementing for loop in MATLAB

821 views (last 30 days)
Jatin Arora
Jatin Arora on 29 Nov 2012
Edited: Dennis M on 24 Aug 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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 29 Nov 2012
for k=5:-1:1
  1 Comment
Dennis M
Dennis M on 24 Aug 2021
Edited: Dennis M on 24 Aug 2021
y = zeros(1,12);
for i = 1:12
y(i+1) = y(i) + ((5 - (4/50)*y(i)));
x = zeros(1,10);
for i = 10:-1:2
x(i-1) = x(i) + ((5 + (4/50)*x(i)));
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,

Sign in to comment.

More Answers (2)

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

Sign in to comment.

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


Find more on Loops and Conditional Statements in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by