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 个评论
采纳的回答
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!