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)
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
Walter Roberson
2021 年 8 月 26 日
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.
Walter Roberson
2021 年 8 月 30 日
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.
Dennis M
2021 年 9 月 2 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
