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 comentarios

Walter Roberson
Walter Roberson el 26 de Ag. de 2021
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
Dennis M el 26 de Ag. de 2021
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

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Ag. de 2021
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 comentarios

Dennis M
Dennis M el 26 de Ag. de 2021
Nice one! That's what I need, Thank you very much, God bless you.
Dennis M
Dennis M el 26 de Ag. de 2021
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
Dennis M el 27 de Ag. de 2021
Pardon Sir, but I didn't know how to insert the loop and What is the value of N?
Walter Roberson
Walter Roberson el 27 de Ag. de 2021
Editada: Walter Roberson el 27 de Ag. de 2021
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
Dennis M el 30 de Ag. de 2021

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
Dennis M el 31 de Ag. de 2021
Thank you very very much Sir Walter, you are really MVP, God bless always
Nikola Manza
Nikola Manza el 1 de Sept. de 2021
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
Walter Roberson el 2 de Sept. de 2021
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
Dennis M el 2 de Sept. de 2021
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
Walter Roberson el 2 de Sept. de 2021
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
Dennis M el 2 de Sept. de 2021
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Just for fun en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Ag. de 2021

Comentada:

el 2 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by