how to build an if statement beyond the conditions

1 view (last 30 days)
Hello Guys,
best wishes to all of us. I beg for help in solving this case. the case is in the form of conditioning logic.
I have to build a program line which is described in the conditions below:
suppose I have an input value in the form of a matrix Q = [0.2; 0.8; 1.4; 1.1; 2; 3; 3.9; 4.2; 5.5; 5; 4.7; 4; 3.5; 3.8; 3.1; 2.5; 1.9; 1.3; 0.7; 0], from the value of this input included in some of the conditions provided below
for input matrix Q uses looping
for example, the first loop, Q = 0.2
CONDITION A * if the value of Q <= 2 then the value of Z = 1200 * if the value of 2 <= Q <= 4 then the value of Z = 2000 * if the value of 4 <= Q <= 6 then the value of Z = 3500
so the value of Z for Q = 0.2 is Z = 1200
and from these conditions, must also meet the following conditions
CONDITION B * if the current Q value is Q value [example in row 4 matrix] then Z value = previous Z value * if the current Q value = from the previous Q value then the Z value is used by the current Z value
and then the program must also meet the following conditions:
CONDITION C * if the Z value in the loop has used the last condition Z (Z = 3500) then the Z value used for looping after that is Z = 3500 [CONDITION A, B, C NOT IN THE PROCESS]
For example in the loop line Q matrix 11 (ie for Q = 3.5) so Z used Z = 3500, not 2000
Please help me
Thanks
  4 Comments
Muhammad Dzul  Akbar
Muhammad Dzul Akbar on 27 Aug 2018
I have tried learning from books and videos from YouTube, but for CONDITION C which is an obstacle for me.
can you give me a clue?
Muhammad Dzul  Akbar
Muhammad Dzul Akbar on 27 Aug 2018
@madhan ravi
for i = 1:size(Q,1); x = Q(i,1); y = Q(i-1,1);
K= zeros(20,1);
if x <= 2
K(i,1) = 1200
elseif x > 2 & x <= 4
K(i,1) = 2000
else x > 4 & x <= 6
K(i,1) = 3500
end
if x < y
K(i,1) = K(i-1,1)
elseif x > y
K(i,1)= K(i,1)
End
End
for condition C, i can't

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 27 Aug 2018
Edited: madhan ravi on 27 Aug 2018
Q = [0.2; 0.8; 1.4; 1.1; 2; 3; 3.9; 4.2; 5.5; 5; 4.7; 4; 3.5; 3.8; 3.1; 2.5; 1.9; 1.3; 0.7; 0]
K= zeros(20,1);
for i = 2:size(Q);
x(i) = Q(i);
y(i) = Q(i-1);
if x(i) <= 2
K(i) = 1200;
elseif x(i) > 2 & x(i) <= 4
K(i) = 2000;
elseif x(i) > 4 & x(i) <= 6
K(i) = 3500;
elseif x(i) < y(i)
K(i) = K(i-1);
elseif x(i)> y(i)
K(i)= K(i);
end
end
K_before = K
for i = 1:length(K)
if K(i)==3500
K(i+1)=K(i);
else
continue
end
end
K_after = K
  12 Comments

Sign in to comment.

More Answers (1)

Pierre845
Pierre845 on 27 Aug 2018
Not sure why you cannot find a solution, it's fairly basic use of a loop.
Do first a if .. then .. end using the matrix Q
Then either a for or while loop using the Z from previous step.
  1 Comment
Muhammad Dzul  Akbar
Muhammad Dzul Akbar on 27 Aug 2018
I have tried to compile the code, but not in accordance with the conditions needed.
could you help me?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!