フィルターのクリア

how to generate modulus of sine wave using if-else statement

2 ビュー (過去 30 日間)
RIZWAN KHALID
RIZWAN KHALID 2017 年 2 月 2 日
回答済み: BOB MATHEW SYJI 2020 年 9 月 12 日
I am trying to generate the modulus of sine wave using if-else statements, but I am not getting the correct waveshape. Codes used in Matlab are as under:
t=linspace(0,2*pi,1000);
i=sin(t);
if i>0
c=i;
else
c=-1*i;
end
plot(t,c)

回答 (1 件)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020 年 9 月 12 日
I think this should work.
t=linspace(0,2*pi,1000);
i=sin(t);
for j=1:length(i)
if i(j)>=0
c(j)=i(j);
else
c(j)=-1*i(j);
end
plot(t,c);
end
but you can use abs function also for ease
t=linspace(0,2*pi,1000);
i=abs(sin(t));
plot(t,i);

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by