Writing the dirac function as a function handle
    7 views (last 30 days)
  
       Show older comments
    
    daniel adams
 on 30 Sep 2021
  
    
    
    
    
    Commented: Steven Lord
    
      
 on 30 Sep 2021
            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? 
0 Comments
Accepted Answer
  Alan Stevens
      
      
 on 30 Sep 2021
        Try
dirac_i = @(x) x==i;   % This assumes i has been fixed before the function is defined 
1 Comment
  Steven Lord
    
      
 on 30 Sep 2021
				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]
g = diracMaker(1); % g's i is 1, but this doesn't affect f's i
[x; g(x)]
f(1:3) % f still remembers its i is 2
More Answers (0)
See Also
Categories
				Find more on Introduction to Installation and Licensing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!