For loop not working. Array indices must be positive integers or logical values.

1 次查看(过去 30 天)
Im Trying to implement an FIR difference equation where the output y is the average sum of a set number of values(M) of the input(x).
f0=10;
fs= 2*50*f0;
ts=1/fs;
tw=2;
t=0:ts:tw;
x=1*square(2*pi*f0*t);
y = zeros(1,length(x));
k=6;
M=k;
for i=1:k
for j = 1:M
y(j) = y +(1/(M+1))*x(i-j);
end
end
figure, plot(t,y, 'k')
ylim([-1 1])

回答(1 个)

James Tursa
James Tursa 2018-12-10
This line on the first iteration when i=1 and j=1:
y(j) = y +(1/(M+1))*x(i-j);
You are indexing x(0). Also, you are trying to assign a vector (y on the rhs) into an element y(j) which isn't going to work either. This needs to be fixed.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by