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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by