signal processing - Incrementing a variable inside a function on MATLAB -
my goal being able graph following:
x(n)= delta(n)+delta(n-1)+delta(n-2)+….+delta(n-10)+delta(n-11)
i have written code:
n = -15:15 e(m) = dirc(n(m)); k = 1 m = 1 : length(n)     while k < 12         e_1(m) = dirc(n(m)-k);         k = k + 1     end     e(m) = e(m) + e_1(m) end  subplot(4,4,5); stem(n,e,'m','markersize',3,'linewidth',1) xlabel('n') ylabel('\delta[n]')  title ('(e)')   and wrote function dirc follows:
function output = dirc(input) output = 0; if input == 0     output = 1; end end   the error
index exceeds matrix dimensions
as can see, terminating function, i'd able graph eventually:
x(n)= delta(n)+delta(n-1)+delta(n-2)+….
you're not showing m in second line, that's probable source of problems. also, you'll have pre-allocate vector e if want increment values in loop, can give out-of-bounds errors.
as rest of code, can replace dirc(input) input==0, resulting logical values compatible numerical 0 , 1 (in case input scalar, seems case you).
as matter of fact, you're trying achieve function
kmax = 11; xfun = @(n) ismember(n,0:kmax);  %// anonymous function x(n) = xfun(n);                  %// evaluate @ specific value n   or if want compute x(n) multiple values of n @ once:
kmax = 11; xfun = @(n) sum(bsxfun(@eq,n(:),0:kmax),2);%// anonymous function xvec = xfun(-15:15);                       %// evaluate @ every n simultaneously      
Comments
Post a Comment