IF Statement with Multiple Conditions and priority

4 ビュー (過去 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