How to enable function is condition is true and ignore its updates till another condition is true

조회 수: 3 (최근 30일)
i need to write this code if input1-input2>5 if condition is true, the output should equal to zero even when input1-input2<5 till input1=input3 then it equal 1 and return to if condition again. in other words if input1-input2>5 is true its update will be ignored till input1=input3

답변 (1개)

Bob Thompson
Bob Thompson 2018년 3월 6일
output = 0;
if input1 == input3;
output = 1;
if input1-input2 > 5;
output = 0;
end
end
You can precondition the output however you want, but by nesting the input1-input2 condition inside of the input1=input3 condition it prevents the condition from being considered until input1=input3.
Alternatively, if you want the condition to be based on the output something like the following could work.
if output == 0;
if input1 == input3;
output = 1;
end
elseif output == 1;
if input1 - input2 > 5;
output = 0;
end
end
This will check for the output condition specifically and ignore the inputs unless the output is a certain value. Fair warning that in both of these conditions the output only updates once per loop, assuming you have a loop, and so will not check for input1=input3 and then input1-input2>5. You could copy the input1input2 if condition into the first output=0 condition (following the input1input3 condition) if you needed it to be considered in the same loop.
  댓글 수: 4
Bob Thompson
Bob Thompson 2018년 3월 6일
You're going to need to run some kind of variable loop inside your if statement if you want to consider multiple situations where your variables do or don't meet a condition. If you would be willing to post some of your code that you are working with I might be able to help more.

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

카테고리

Help CenterFile Exchange에서 Multibody Modeling에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by