필터 지우기
필터 지우기

Create a loop with decrementing step

조회 수: 4 (최근 30일)
Dennis M
Dennis M 2021년 8월 25일
댓글: Dennis M 2021년 9월 2일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by