What does this command (x = [(n-n0) == 0];) do in the following coding on Matlab?

13 views (last 30 days)
function [x, n] = impseq(n0, n1, n2)
n = [n1:n2];
x = [(n-n0) == 0];

Accepted Answer

Joseph Cheng
Joseph Cheng on 2 Sep 2014
Edited: Joseph Cheng on 2 Sep 2014
if we break it down into sections:
n-n0==0
will do the logical comparison of the array n-n0 to 0. This will result in a logical array with 1 where the index inside n-n0 is 0 and the rest 0. Then that result is stored in x.

More Answers (2)

Image Analyst
Image Analyst on 2 Sep 2014
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.

Guillaume
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.

Community Treasure Hunt

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

Start Hunting!