If statement with two conditions

Hello everyone, once again.
Please I need a fix for this code, I get the same answer for each condition.
function out = select(condition, a, b)
if a > b == condition
out = a;
else
out = b;
end
How can I fix this?

5 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 13 日
explicitly state your inputs
Adam
Adam 2019 年 2 月 13 日
The comments for your function don't make sense. What is int2 that keeps being referred to and what is the point of 'condition' and what is it? a logical? char? double? The code suggests it would be a logical though why you would do this is not at all understandable!
Amos Agbetile
Amos Agbetile 2019 年 2 月 13 日
Oh, sorry bro, i've edited it, and removed the comment.
Guillaume
Guillaume 2019 年 2 月 13 日
Yes, as Adam said, the comments don't make sense, and it's very unclear what you're trying to achieve. Your current code is exactly equivalent to:
if condition
if a > b
out = a;
else
out = b;
end
else
if ~(a > b)
out = a;
else
out = b;
end
end
assuming condition, a, and b are scalars (if they're vectors the whole logic is a lot more complicated). Whether or not, it's what you wanted to achieve is unknown.
I get the same answer for each condition
No, you will get a different result for condition = true than for condition = false (or 0 or 1), unless a and b are equal of course.
Note that if either of a, b or condition are vectors, then vector rules apply to if and yes, it's very likely the if is always false.
Amos Agbetile
Amos Agbetile 2019 年 2 月 13 日
Thank you Guillaume, thank you all.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2019 年 2 月 13 日

コメント済み:

2019 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by