フィルターのクリア

if statement problem with function

5 ビュー (過去 30 日間)
Khadijat Chagayeva
Khadijat Chagayeva 2020 年 10 月 6 日
コメント済み: Stephen23 2020 年 10 月 9 日
i'm given the task to create an if statement for this problem
i have an array from -1:1
x=[-1:0.1:1]
and a function f= x.^2.*sin(pi.*x)
and i'm supposted to make an if statement arround g
if F>=0 then g=F
if F<0 then g=0
the problem seems pretty easy to solve but somehow i can't seem to do it
i've coded this so far but i keep getting error messages and i don't understand why it's not working
i keep getting the error message: Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in solution (line 6)
if f(1,i) >= 0
x = [-1:0.1:1];
f = (x.^2).*(sin(pi.*x));
for i = -1:1
if f(1,i) >= 0
g(1,i)=f
elseif f(1,i)<0
g(1,i)=0
end
end
  1 件のコメント
Stephen23
Stephen23 2020 年 10 月 6 日
The MATLAB approach:
x = -1:0.1:1; % get rid of the superfluous brackets
f = x.^2.*sin(pi.*x);
g = max(0,f);

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

採用された回答

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 6 日
This may help you:
x = [-1:0.1:1];
f = (x.^2).*(sin(pi.*x));
g=zeros(1,length(f));
for i = 1:length(f)
if f(i) >= 0
g(i)=f(i);
elseif f(i)<0
g(i)=0;
end
end
  3 件のコメント
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 8 日
this is inialization of 'g'.
Stephen23
Stephen23 2020 年 10 月 9 日

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

その他の回答 (0 件)

カテゴリ

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