Now my question what should I add in that code to make the code display 0 on the values less than 0 because now with my code it goes down to the negative values.Thank you.

조회 수: 2 (최근 30일)
My unit ramp function was r[n] = 0 when n<0 and n when 4=<n>=9. I had to code and display the results of this Ramp function. And this was my code.
n=(-20:1:20)';
ramp_n((n<=4).*n+(n>=9).*n);
Unrecognized function or variable 'ramp_n'.
stem(n,ramp_n);

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 18일
You should be multiplying logical conditions, not adding them.
  댓글 수: 2
khumbula higa
khumbula higa 2022년 5월 18일
I tried that and my code was like below and they all become zeros now ,even the ones that were supposed to form a remp.
n=(-20:1:20)';
ramp_n=((n<=4).*n.*(n>=9)).*n);
stem(n,ramp_n);
Walter Roberson
Walter Roberson 2022년 5월 19일
  1. Remember that multiplication is commutative, so what you have coded there is equivalent to (n<=4).*(n>=9).*(n.*n) which is obviously not correct, as it would output either 0 or n^2 rather than 0 or n . You should only have had one multiplication by n
  2. You want n between 4 and 9, but (n<=4) is true (1) when n is less than 4 -- a condition in which you want 0 to be output, not 1. You need your condition to be true (1) when n is inside the desired range, not when it is outside the desired range. (4<= n & n <= 9)
n = (-20:1:20)';
ramp_n = (4<=n).*(n<=9).*n;
stem(n, ramp_n);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cash Flows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by