how to build an if statement beyond the conditions

1 次查看(过去 30 天)
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 个评论
Muhammad Dzul  Akbar
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
@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

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2018-8-27
编辑:madhan ravi 2018-8-27
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 个评论

请先登录,再进行评论。

更多回答(1 个)

Pierre845
Pierre845 2018-8-27
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 个评论
Muhammad Dzul  Akbar
I have tried to compile the code, but not in accordance with the conditions needed.
could you help me?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by