필터 지우기
필터 지우기

IF Statement with Multiple Conditions and priority

조회 수: 7 (최근 30일)
IDN
IDN 2021년 12월 16일
답변: Walter Roberson 2021년 12월 16일
I have a set of conditions but one has priority over the other
example:
Data = zeros(size(V));
for t = 1:numel(Data)
if (V>=MAV&XRDiff>0&PXR<=5&XR>=5) %//// Statement1:If this conditions are met entirely then output 1
Data = 1;
if (XR>=10) %/// Statement2:if Satement 1 is False then evalute condition
Data = -1; %//// Statement2:If this conditions are met entirely then output -1
Data = 0; %//// If Statement 2 is FALSE, it means also Statement 1 was FALSE and then i want it to be zero
end
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 12월 16일
"Statement2:if Satement 1 is False then evalute condition"
You would have had to use an else to reach statement 2 when statement 1 was false.

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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 12월 16일
In the C programming language, the scope of an if body is a statement that might be a simple statement or might be a compound block inside {} . So in C if you had
if (A)
this();
that();
then the scope of the if would have ended after the this(); call, and no else is involved, so that() would be called no matter whether the condition is true or false. Those C statements would be equivalent, in C, to
if (A)
{this();}
else
{};
that();
But even in C, an else is not implied by being the next statement after an if body.
In MATLAB, though, the scope of an if body is until the first matching else or elseif or end statement. So in MATLAB,
if (A)
this();
that();
end
would call both this() and that() if the condition (A) is true, and no else would be called. To get an else in MATLAB you need to specify it:
if (A)
this();
else
that();
end

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by