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

12 ビュー (過去 30 日間)
Lahiru
Lahiru 2014 年 9 月 2 日
コメント済み: Bruce lee 2019 年 3 月 15 日
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