how to build an if statement beyond the conditions

조회 수: 5 (최근 30일)
Muhammad Dzul  Akbar
Muhammad Dzul Akbar 2018년 8월 27일
편집: madhan ravi 2018년 8월 28일
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
Muhammad Dzul Akbar 2018년 8월 27일
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 2018년 8월 27일
@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
Muhammad Dzul  Akbar
Muhammad Dzul Akbar 2018년 8월 28일
Ok thanks @madhan ravi, it works correctly
madhan ravi
madhan ravi 2018년 8월 28일
Your welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (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
Muhammad Dzul Akbar 2018년 8월 27일
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