Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
matlabgod
matlabgod am 10 Dez. 2018
Erneut geöffnet: Walter Roberson am 20 Dez. 2018
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])

Antworten (1)

James Tursa
James Tursa am 10 Dez. 2018
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.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by