Decrementing step for loop I matlab

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

Réponses (1)

Awais Saeed
Awais Saeed le 24 Août 2021
Modifié(e) : Awais Saeed le 24 Août 2021
% start, step, and end points
starty = 1;
endy = 12;
startx = 10;
endx = 2;
decrement = -1;
y = zeros(1,12);
for i = starty:endy
y(i+1) = y(i) + ((5 - (4/50)*y(i)));
end
y
x = zeros(1,10);
for i = startx:decrement:endx
x(i-1) = x(i) + ((5 + (4/50)*x(i)));
end
x

3 commentaires

Dennis M
Dennis M le 24 Août 2021
Thanks, but want a variable of start and end. For example : 17 to 30 or 20 to 45; 15 to 2 or 60 to 40 with decrementing step.
Awais Saeed
Awais Saeed le 24 Août 2021
Modifié(e) : Awais Saeed le 24 Août 2021
I am having difficulty in trying to understand your question. Do you want your first loop to start from 17 or 20 and end at 30 or 45? If that is the case just do
starty = 17;
endy = 30;
Same goes for the second loop. 'starty' and 'endy' are variables.
Dennis M
Dennis M le 24 Août 2021

<<

>> There was error when I change the variables, I have to change also the y = zeros(1,12); defend on the length. I want a loop that the output is like this (10 20 29 38 47 56 65 and so on) and (80 70 61 53 46 40 35 and so on) same in negative (-10 -20 -29 -38 -47 -56 -65 and so on) and (-80 -70 -61 -53 -46 -40 -35 and so on)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 24 Août 2021

Commenté :

le 24 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by