What does this command (x = [(n-n0) == 0];) do in the following coding on Matlab?
Show older comments
function [x, n] = impseq(n0, n1, n2)
n = [n1:n2];
x = [(n-n0) == 0];
Accepted Answer
More Answers (2)
Image Analyst
on 2 Sep 2014
2 votes
n1:n2 creates an array from n1 to n2 in steps of 1, for example 3:7 is the array [3,4,5,6,7]. So that would be "n" upon returning to the main calling routine.
n-n0 gives an array of the difference of n and n0. For the above example if n1=3 and n2=7 and n0 = 5, then n-n0 = [-2, -1, 0, 1, 2]. Saying == 0 gives a logical (boolean) array that is true when the array = 0, for this example it would be x = [0, 0, 1, 0, 0] because only the middle element is zero.
1 Comment
Bruce lee
on 15 Mar 2019
Thank you! Your answer is pretty good!
Guillaume
on 2 Sep 2014
It certainly is a convoluted way to write:
x = (n == n0);
Whoever wrote this should have added a comment explaining why they went at it in a roundabout way. I would strongly advise you to avoid writing code like this.
Categories
Find more on Logical 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!