Reputation: 95
My goal is being able to graph the following:
x(n)= delta(n)+delta(n-1)+delta(n-2)+….+delta(n-10)+delta(n-11)
I have written this code:
n = -15:15
e(m) = dirc(n(m));
k = 1
for 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 I wrote the function dirc as follows:
function output = dirc(input)
output = 0;
if input == 0
output = 1;
end
end
The error is
Index exceeds matrix dimensions
As you can see, this is a terminating function, I'd like to be able to graph something like this eventually:
x(n)= delta(n)+delta(n-1)+delta(n-2)+….
Upvotes: 0
Views: 390
Reputation: 35109
You're not showing us what m
is in the second line, that's a probable source of your problems. Also, you'll have to pre-allocate the vector e
if you want to increment its values in a loop, this can also give you out-of-bounds errors.
As for the rest of your code, you can replace dirc(input)
with simply input==0
, the resulting logical values will be compatible with numerical 0
and 1
(in case the input
is scalar, which seems to be the case for you).
As a matter of fact, what you're trying to achieve is the function
kmax = 11;
xfun = @(n) ismember(n,0:kmax); %// anonymous function
x(n) = xfun(n); %// evaluate at any specific value n
or if you want to compute x(n)
for multiple values of n
at once:
kmax = 11;
xfun = @(n) sum(bsxfun(@eq,n(:),0:kmax),2);%// anonymous function
xvec = xfun(-15:15); %// evaluate at every n simultaneously
Upvotes: 2