Create a loop with decrementing step

Good Day, May I ask a question, how can I create 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)
Thanks

 採用された回答

the cyclist
the cyclist 2021 年 8 月 25 日

2 投票

Do you mean that you want the increment to be 10, 9, 8, ...? (That is not what your first example does). Here is one way to do a series like that. I hope you can generalize to what you need.
inc = 10:-1:1;
step = cumsum(inc)
step = 1×11
10 19 27 34 40 45 49 52 54 55 55

6 件のコメント

Dennis M
Dennis M 2021 年 8 月 25 日
Nice... Thanks, both my example has decrementing step and I want that start, end and step was variable example [10 (+10) 20 (+9) 29 (+8) 37 and so on] what if [80 (-10) 70 (-9) 61 (-8) 53 (-7) 46 and so on] what if I want [50 (+5) 55 (+4) 59 (+3) 62 and so on]
Dennis M
Dennis M 2021 年 8 月 25 日
and I want it in a loop that can display one by one
the cyclist
the cyclist 2021 年 8 月 25 日
inc = 10:-1:1;
step = cumsum(inc);
for s = step
s
end
s = 10
s = 19
s = 27
s = 34
s = 40
s = 45
s = 49
s = 52
s = 54
s = 55
Dennis M
Dennis M 2021 年 8 月 25 日
Many Thanks, the cyclist your very responsive
what if I want like this [80 (-10) 70 (-9) 61 (-8) 53 (-7) 46 and so on]
and I want also a step that vary like this [50 (+5) 55 (+4) 59 (+3) 62 and so on]
the cyclist
the cyclist 2021 年 8 月 27 日
I would suggest that you really try to understand my other solution. If you did, then it is trivial how to figure out this question.
inc = -10:1:1;
step = cumsum([80 inc])
step = 1×13
80 70 61 53 46 40 35 31 28 26 25 25 26
I suggest you study the MATLAB Onramp online tutorial. You will learn how to do simple tasks like this one.
Dennis M
Dennis M 2021 年 9 月 2 日
Good Day,
Sir The Cyclist,
Here's my script please check if there's improvement, thanks
clear all
format compact
finalstep = 0.125;
%%
starta = 20;
stopa = 30;
hysa = abs(stopa - starta);
stastep = hysa * 0.2;
stepa = linspace(finalstep,stastep,hysa);
A = stopa - cumsum([0 stepa]);
A = A(A >= starta);
intermediatesa = flipud(A.').';
for loop_variablea = intermediatesa
%body of the loop goes here
a = loop_variablea
end
%%
startb = 15;
stopb = 5;
hysb = abs(stopb - startb);
stastep = hysb * 0.2;
stepb = linspace(finalstep,stastep,hysb);
B = stopb + cumsum([0 stepb]);
B = B( B <= startb);
intermediatesb = flipud(B.').';
for loop_variableb = intermediatesb
%body of the loop goes here
b = loop_variableb
end
%%
startx = -20;
stopx = -30;
hysx = abs(stopx - startx);
stastep = hysx * 0.2;
stepx = linspace(finalstep,stastep,hysx);
X = stopx + cumsum([0 stepx]);
X = X(X <= startx);
intermediatesx = flipud(X.').';
for loop_variablex = intermediatesx
%body of the loop goes here
x = loop_variablex
end
%%
starty = -15;
stopy = -5;
hysy = abs(stopy - starty);
stastep = hysy * 0.2; % start step
stepy = linspace(finalstep,stastep,hysy);
Y = stopy - cumsum([0 stepy]);
Y = Y(Y >= starty);
intermediatesy = flipud(Y.').';
for loop_variabley = intermediatesy
%body of the loop goes here
y = loop_variabley
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeJust for fun についてさらに検索

質問済み:

2021 年 8 月 25 日

コメント済み:

2021 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by