Hello, Im trying to create a top hat function (that is 0 everywhere except between -halfpix & halfpix). the fucll width of the top hat (2* halfpix) is symetrical about the yaxis.
i've created it based in discrete values as below but I would like it as a general function.
x = -20:0.1:20;
t = zeros(1,length(x))
t(x>=-halfpix & x<halfpix) = 1;

 採用された回答

Rik
Rik 2021 年 6 月 28 日

0 投票

If you realize that you are marking all true locations with 1 and leaving all false as 0, you can use that to make an anonymous function that only casts you logical statement to double.

5 件のコメント

Jason
Jason 2021 年 6 月 28 日
do you mean have an if statement in the anonymous functio, I didnt think this was possible
Rik
Rik 2021 年 6 月 28 日
You don't even need an if, just double:
x=1:10;
double(x>5)
ans = 1×10
0 0 0 0 0 1 1 1 1 1
Jason
Jason 2021 年 6 月 28 日
But thats if x>5, id also want if x<-5
Rik
Rik 2021 年 6 月 28 日
You already know how to do that. You already used the code in your own question:
t=@(x) double(x>=-halfpix & x<halfpix)
Jason
Jason 2021 年 6 月 28 日
Perfect, thankyou

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

質問済み:

2021 年 6 月 28 日

コメント済み:

2021 年 6 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by