how can i draw input rectangular shape with specific iterations

1 回表示 (過去 30 日間)
sandy jeak
sandy jeak 2015 年 12 月 19 日
コメント済み: Star Strider 2015 年 12 月 19 日
n=100
for k=1:n
if k<=25
r(k)=1
elseif k>25&k<=50
r(k)=-1
elseif k>50&k<=75
r(k)=1
else
r(k)=-1
end
end
plot (k,r(k))
This appears when running:
But I want this figure:
  2 件のコメント
Star Strider
Star Strider 2015 年 12 月 19 日
Image Analyst
Image Analyst 2015 年 12 月 19 日
In general, you would not use & in that situation (your "if" test), you'd use &&, though in this case it won't matter.

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

回答 (2 件)

Star Strider
Star Strider 2015 年 12 月 19 日
sqwv = @(x) 1.*(((x > 0) & (x < 25)) | ((x > 50) & (x < 75))) - 1.*(((x > 25) & (x < 50)) | ((x > 75) & (x < 100)));
x = 0:100;
y = sqwv(x);
figure(1)
plot(x, y,'-b')
axis([0 100 -1.5 1.5])

Image Analyst
Image Analyst 2015 年 12 月 19 日
You can use repmat(), like I do in my attached demo.
Or, if you have the Signal Processing Toolbox, you can use the square() function:
x = square(t,duty) generates a square wave with specified duty cycle, duty, which is a number between 0 and 100. The duty cycle is the percent of the period in which the signal is positive.
  1 件のコメント
Star Strider
Star Strider 2015 年 12 月 19 日
I did a version of the repmat approach in how can i draw this figure earlier.

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

カテゴリ

Help Center および File ExchangePulse and Transition Metrics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by