How to use for or while loop for certain range?

I want to add or substract a certain number to my array cell (result(i)) based on corresponding binary(0 or 1) pattern I have . Condition: if binary_pattern is 1 then it should increase with speed of 0.75 per minute, if zero it should decrease with the same speed(-0.75). But when binary_pattern is 1 and inside the range 15 and 20 it should increase by 1 not 0.75. Which means when result reaches 20, has to decrease by and increase again when hits 15 with the speed of 1. Any advices on that? I have tried with the code below, but due to I'm new to Matlab couldn't get my head around it. Thanks in advance!
binary_pattern = zeros(100,1);
binary_pattern(1:25) = 1;
binary_pattern(50:75) = 1;
binary_pattern(90:100) = 1; % to create binary pattern
minutes = [1:100]'; % scale of minutes
result = zeros(100,1);
result(1,1) = 5; % initial condition of a state
for i = 1:100
if binary_pattern(i) == 1
result(i+1) = result(i) + 0.75;
if result(i)>= 20
result(i+1) = result(i) - 1;
elseif result(i)<= 15
result(i+1) = result(i) + 1;
else
end
else
result(i+1) = result(i) - 0.75;
end
end
result(end,:) = [];
plot(minutes,result)

 Respuesta aceptada

binary_pattern = zeros(100,1);
binary_pattern(1:25) = 1;
binary_pattern(50:75) = 1;
binary_pattern(90:100) = 1; % to create binary pattern
minutes = [1:100]'; % scale of minutes
result = zeros(100,1);
result(1,1) = 15; % initial condition of a state
DeS = 0;
for i = 1:100
if binary_pattern(i) == 1
result(i+1) = result(i) + 1;
if DeS == -1
result(i+1) = result(i) - 1;
end
if result(i+1)>20
result(i+1)= result(i) - 1;
DeS = -1;
end
if result(i+1)<15
result(i+1) = result(i) + 1;
DeS = 0;
end
else % pattern is 0
result(i+1) = result(i) - 0.75;
end
end
result(end,:) = [];
plot(minutes,result)

Más respuestas (1)

SC
SC el 30 de Jun. de 2020
I've edited the code, according to your logic (from what I've understood)
(Try explaining your logic better)
see if this works, incase:
zerosmatrix = zeros(100,1);
zerosmatrix(1:25) = 1;
zerosmatrix(50:75) = 1;
zerosmatrix(90:100) = 1;
timeframe = [1:100]';
state = zeros(100,1);
for i = 1:100
if zerosmatrix(i) == 1
state(i+1) = state(i) + 0.75;
if state(i)>= 15 && state(i)<= 20
state(i+1) = state(i) + 1;
elseif state(i)>= 20
state(i+1) = state(i) - 1;
end
else
state(i+1) = state(i) - 0.75;
end
end
state(end,:) = [];
plot(timeframe,state)

5 comentarios

Asrorkhuja Ortikov
Asrorkhuja Ortikov el 30 de Jun. de 2020
Editada: Asrorkhuja Ortikov el 30 de Jun. de 2020
@SC You are right, I have edited my question. Have a look, hopefully this time is much more understandable. Sorry for confusion. And no the code is working almost same as mine. The thing is when result reaches 20 (max) it should fluctuate between 15 and 20 with the same speed, in our case 1 or -1 until the pattern changes to zero.
Your logic won't hit 15 ever, because in the range of 15 to 20 you want it to increase but when it hits 20 you want it to decrease to go to 15? it won't.. immediately after 20 is decreased by 1 it will be around 19 and 19 is in te range of 15 & 20.. which will start increasing again.. I've written a while loop to satisfy your logic, check this once:
binary_pattern = zeros(100,1);
binary_pattern(1:25) = 1;
binary_pattern(50:75) = 1;
binary_pattern(90:100) = 1; % to create binary pattern
minutes = [1:100]'; % scale of minutes
result = zeros(100,1);
result(1,1) = 5; % initial condition of a state
for i = 1:100
if binary_pattern(i) == 1
if result(i)>= 15 && result(i)< 20
result(i+1) = result(i) + 1;
elseif result(i) >= 20 && result(i) < 21
while result(i)>= 15
result(i+1) = result(i) - 1;
i = i+1;
end
elseif result(i) < 15 || result(i) >= 21
result(i+1) = result(i) + 0.75;
end
else
result(i+1) = result(i) - 0.75;
end
end
result(end,:) = [];
plot(minutes,result)
@SC it's still bouncing back from 19
SC
SC el 1 de Jul. de 2020
Hey that is because of the binary pattern, please try another binary pattern and check.
@ SC I did, but it is still not working? Did you check it yourself first? Here I took different approach, have a look:
binary_pattern = zeros(100,1);
binary_pattern(1:25) = 1;
binary_pattern(50:75) = 1;
binary_pattern(90:100) = 1; % to create binary pattern
minutes = [1:100]'; % scale of minutes
result = zeros(100,1);
result(1,1) = 15; % initial condition of a state
DeS = 0;
for i = 1:100
if binary_pattern(i) == 1
result(i+1) = result(i) + 1;
if DeS == -1
result(i+1) = result(i) - 1;
end
if result(i+1)>20
result(i+1)= result(i) - 1;
DeS = -1;
end
if result(i+1)<15
result(i+1) = result(i) + 1;
DeS = 0;
end
else % pattern is 0
result(i+1) = result(i) - 0.75;
end
end
result(end,:) = [];
plot(minutes,result)

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by