Having trouble creating a conditional function

12 ビュー (過去 30 日間)
Michael Bettar
Michael Bettar 2017 年 10 月 17 日
コメント済み: Walter Roberson 2017 年 10 月 17 日
Hi it's my first time posting here
I am trying to create a conditional function
x = -1:0.1:1;
f = x.^2.*sin(pi.*x);
So the conditions are
if f>=0
g = f
elseif f<0
g = 0
I have tried to create separate vectors containing the index that give positive respespective negative values of f and then create an if statement that looked something like this
if f == f(a)
g = f(a)
%where a is the index that give positive values
elseif f == f(b)
g = 0
end
but it doesn't seem to work at all
what am I doing wrong?

回答 (1 件)

KL
KL 2017 年 10 月 17 日
編集済み: KL 2017 年 10 月 17 日
Just use
x = -1:0.1:1;
f = x.^2.*sin(pi.*x);
and then to create g based on f
g = f;
g(g<0)=0;
  2 件のコメント
Michael Bettar
Michael Bettar 2017 年 10 月 17 日
wow I really have overcomplicated the answer
thanks for help!
Walter Roberson
Walter Roberson 2017 年 10 月 17 日
g = max(0, f);

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by