MATLAB: Attempted to access x(2); index out of bounds because numel(x)=1.

attempted to access x(2)MATLAB

it's my code:
clear all;
N=1000;
a1=-1.7559760050623385;
a2=0.82149794841686108;
b0=1;
b1=-0.83683008015976024;
b2=1;
coeff=0.039878940119824811;
x=sin(2*pi*(N-1))/N*3;
z0=0;
z1=0;
z2=0;
for n=1:N
z2=z1;
z1=z0;
z0=x(n)-a1*z1-a2*z2;
y(n)=(b0*z0+b1*z1+b2*z2)*coeff;
end
figure(1);
plot(x);
figure(2);
plot(y);
An error
Attempted to access x(2); index out of bounds
because numel(x)=1.
Error in ==> Untitled at 20
z0=x(n)-a1*z1-a2*z2;
How to solve a problem?

Best Answer

change
z0=x(n)-a1*z1-a2*z2;
to
z0=x-a1*z1-a2*z2;
Attempted to access x(2) means you're trying to access the 2nd element of x while x only has one element ( because numel(x)=1), hence index out of bounds.
Related Question