loop with decrementing step
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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)
The start, end and step is variable
example I can start in 17 to 30 and start step is 5 to 0.125 just like this [17 (+5) 22 until 29.875 (+0.125) 30] same in negation
댓글 수: 2
Walter Roberson
2021년 8월 26일
What would be the rule for that last example? How does the step of +5 get down to +0.125 ?
The other ones, the step just decreases by +1 each time for increasing series, or the step increases by 1 each time (from -9 to -8 to -7 and so on) for decreasing series... but how +5 gets to be +0.125 is not at all obvious.
Dennis M
2021년 8월 26일
Thanks Sir Walter, I want start, end, and step to be variable and the step is decrementing with declare value when it reach the end value. Example the start step is 5 end of 0.125 when reach the desire end value. Illustrated code A= start:step:end A= 17:(linspace 5,0.125):30
채택된 답변
Walter Roberson
2021년 8월 26일
start = 17;
step = linspace(5,0.125);
stop = 30;
ZZZZZZZZZ_intermediates = start + cumsum([0 step]);
if start <= stop
ZZZZZZZZZ_intermediates = ZZZZZZZZZ_intermediates(ZZZZZZZZZ_intermediates<=stop);
else
ZZZZZZZZZ_intermediates = ZZZZZZZZZ_intermediates(ZZZZZZZZZ_intermediates>=stop);
end
for loop_variable = ZZZZZZZZZ_intermediates
%body of the loop goes here
disp(loop_variable)
end
17
22
26.9508
You might notice that the loop ended before 29.875 . You asked for the increments to be linspace(5,0.125) but remember by default linspace() is a vector of 100 values -- so the first increment would be 5, the second would be about 4.9508, the third is about 4.9501, and so on. Those quickly add up to more than the stop point.
Your requirement to be able to specify an expression for the increments, leads to the possibility that your increments might have a mix of positive and negative numbers. A decision had to be made about what to do for the case where the steps increment past the endpoint but that steps with the opposite sign then take the value back to below the endpoint. Clearly values beyond the end point should not be included... but should you stop as soon as you pass the endpoint the first time, or should you allow for the value to come back?
I decided that since it should be permitted to allow a mix of increments while you are in range, that it would make the most sense to allow values to return back to range. So what I do is filter out the values that go beyond the stop-point -- leaving any that come back to the range. So with positive increments 5 for a while, and negative increments (say) 7, a sequence with start 1 and stop 20 and increments [5 5 5 5 -7 -7] might go [1 6 11 16 (OMITTED 21) 14 7] leaving [1 6 11 16 14 7] as the loop control values.
It might also be reasonable to argue that the sequence should never emit values "before" the start after having gone past it -- so 1:[5 5 5 5 -7 -7 -7]:20 with the logic posted above would give [1 6 11 16 14 7 0] but it would be reasonable to filter on start as well as stop giving loop values [1 6 11 16 14 7] ...
댓글 수: 13
Dennis M
2021년 8월 26일
Nice one! That's what I need, Thank you very much, God bless you.
Dennis M
2021년 8월 26일
May I request a little help, is it possible to make the last step will be 0.125 or 0.0312 to the end variable? I use it in ramping a current so that I need it precise step. Thanks
is it possible to make the last step will be 0.125 or 0.0312 to the end variable
No, not without a redesign in which you stated actual specifications instead of my having to guess about what has to be handled (or not.)
If you are ramping a current, then consider the possibility of using nonlinear steps. For example, assuming postive start and stop
pre_stop = stop - 0.125;
ZZZZZZZZZ_intermediates = [logspace(log(start), log(pre_stop), N-1), stop];
Dennis M
2021년 8월 27일
Pardon Sir, but I didn't know how to insert the loop and What is the value of N?
Walter Roberson
2021년 8월 27일
편집: Walter Roberson
2021년 8월 27일
Here, N is the number of steps including the first and the last.
format long g
start = 17;
stop = 30;
last_step = 0.125;
N = 15; %number of steps
pre_stop = stop - last_step;
if start < 0 || pre_stop < 0
error('This code version can only deal with positive start and stop')
end
ZZZZZZZZZ_intermediates = [logspace(log10(start), log10(pre_stop), N-1), stop];
if start <= stop
ZZZZZZZZZ_intermediates = ZZZZZZZZZ_intermediates(ZZZZZZZZZ_intermediates<=stop);
else
ZZZZZZZZZ_intermediates = ZZZZZZZZZ_intermediates(ZZZZZZZZZ_intermediates>=stop);
end
for loop_variable = ZZZZZZZZZ_intermediates
%body of the loop goes here
disp(loop_variable)
end
17
17.7535099752308
18.540418614154
19.3622062830199
20.2204189640041
21.116671163576
22.0526489497773
23.0301131241255
24.0509025341092
25.1169375325065
26.2302235900354
27.392855068131
28.6070191589479
29.875
30
Dennis M
2021년 8월 30일
Can I request more? Can I have 50 to 40, or -17 to -30, or -50 to -40? With the last step is 0.125.
In the case of positive values in which the order is decreasing, then
pre_stop = stop - last_step;
would instead be
pre_stop = stop + last_step;
so you could handle both of those cases with
pre_stop = stop - sign(stop-start) * last_step;
To handle the case where both start and stop are positive, or both are negative, then you could try
ZZZZZZZZZ_intermediates = [sign(start)*logspace(log10(abs(start)), log10(abs(pre_stop)), N-1), stop];
but you should double-check that.
That code will not function properly if start and stop are opposite signs.
Dennis M
2021년 8월 31일
Thank you very very much Sir Walter, you are really MVP, God bless always
Nikola Manza
2021년 9월 1일
Hi Sir Walter,
I notice that when I start to 17 to 30 the step was incrementing and when I start to 30 to 17 the step was decrementing. How can make it both step is decrementing. Thanks
Walter Roberson
2021년 9월 2일
How can make it both step is decrementing
You cannot do that without a redesign in which you stated actual specifications instead of my having to guess about what has to be handled (or not.) The specifications need to specifically discuss:
- what to do if both values are positive and increasing order
- what to do if both values are positive and decreasing order
- what to do if both values are negative and increasing order (more negative then less negative)
- what to do if both values are negative and decreasing order (less negative then more negative)
- what to do if the first value is negative and the second value is positive
- what to do if the first value is positive and the second value is negative
- what, more precisely, is the shape of the curve that needs to be followed in changing the step size
Please do not point me to old comments to say you already did that: the old comments were inconsistent about how to change the step size, with some of them using a constant step and others using a changing step. We need a new statement of what the shape needs to look like.
Good Day,
Sir Walter, please check my script if there's improvement.
I used flipud(A.').' in your comment to other question.
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
Walter Roberson
2021년 9월 2일
No, there is no point in my reading that code. It clearly does not answer the points I raised in https://www.mathworks.com/matlabcentral/answers/1441094-loop-with-decrementing-step#comment_1716959 . I have negative interest in trying again work out your requires by working backwards from your work.
If you want further assistance, you need to make clear you need the code to do. You have to ask the right question, because I am tired of giving answers to what you just maybe meant and then being told Yes / No without even a "Warmer / Colder" to guide me.
Dennis M
2021년 9월 2일
Pardon Sir Walter I'm confusing also what the best code or possiblity to do, but you make it by example and I really appreciate that.
I combine your previous code and comment to other question. Now it's clear to me that important was the last step and just flip it.
Thanks Again!
I Hope You will answer my question again in the future
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Just for fun에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
