Writing the dirac function as a function handle

조회 수: 6 (최근 30일)
daniel adams
daniel adams 2021년 9월 30일
댓글: Steven Lord 2021년 9월 30일
Hi I want to write the dirac function at $i$ as a function handle, for example the code will look something like this
dirac_i = @(x) 1 if x=i
otherwise 0
end
But I know I cant use if statements in my function handle, so how should I do this?

채택된 답변

Alan Stevens
Alan Stevens 2021년 9월 30일
Try
dirac_i = @(x) x==i; % This assumes i has been fixed before the function is defined
  댓글 수: 1
Steven Lord
Steven Lord 2021년 9월 30일
If i was not fixed but you want a function handle that makes the appropriate function handle:
diracMaker = @(i) @(x) x == i;
To use call diracMaker to "lock in" the value of i then call the function handle diracMaker returned to evaluate the Dirac "function".
f = diracMaker(2); % f is a function handle that "remembers" its i is equal to 2
x = -5:5;
y = f(-5:5); % Use f
[x; y]
ans = 2×11
-5 -4 -3 -2 -1 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 0
g = diracMaker(1); % g's i is 1, but this doesn't affect f's i
[x; g(x)]
ans = 2×11
-5 -4 -3 -2 -1 0 1 2 3 4 5 0 0 0 0 0 0 1 0 0 0 0
f(1:3) % f still remembers its i is 2
ans = 1×3 logical array
0 1 0

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Collections에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by