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

12 次查看(过去 30 天)
function [x, n] = impseq(n0, n1, n2)
n = [n1:n2];
x = [(n-n0) == 0];

采纳的回答

Joseph Cheng
Joseph Cheng 2014-9-2
编辑:Joseph Cheng 2014-9-2
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.

更多回答(2 个)

Image Analyst
Image Analyst 2014-9-2
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 2014-9-2
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!

Translated by