Simple if statement always gives 0

I'm trying to write a simple function which makes a value either 1 or 0 depending on the input. It looks like this:
function f = W(t)
if 0 <= t < (3/12)
f = 0;
end
if (3/12) <= t <= (8/12)
f = 1;
end
if (8/12) < t < 1
f = 0;
end
if t >= 1
t = t - 1;
end
end
However, no matter what the input is, it always returns zero. What's causing this?

 採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 6 月 5 日

0 投票

You need to put && between two comparisons as:
function f = W(t)
if 0 <= t && t < (3/12)
f = 0;
end
if (3/12) <= t && t <= (8/12)
f = 1;
end
if (8/12) < t && t < 1
f = 0;
end
if t >= 1
t = t - 1;
end
end
Also note that the last comparison is not returning anything

1 件のコメント

Tom Oud
Tom Oud 2019 年 6 月 5 日
Fixed! Thanks a lot!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 6 月 5 日

コメント済み:

2019 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by