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.

카테고리

Help CenterFile Exchange에서 Signal Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by