Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
matlabgod
matlabgod le 10 Déc 2018
Rouvert : Walter Roberson le 20 Déc 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])

Réponses (1)

James Tursa
James Tursa le 10 Déc 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by