Azzera filtri
Azzera filtri

Please help with Index exceeds matrix dimensions error

2 visualizzazioni (ultimi 30 giorni)
%%here is my code
clc; close all;
N=50;
x = zeros(1,N);
for n = 1:length(x)
x(n)= sin((n/16)*pi);
end
y= zeros(1,N);
y(1)=-0.13;
for n = 2:length(x)
y(n+1)=(-1/3)*x(n+2)+(1/2)*x(n+1)+(1/3)*x(n)+y(n);
end
stem(0:1:50, y, 'r-');
xlabel('n');
ylabel('y[n]');
title('Output y[n] of the system');
pause;

Risposta accettata

Star Strider
Star Strider il 3 Dic 2017
This will eliminate the error:
for n = 2:length(x)-2
y(n+1)=(-1/3)*x(n+2)+(1/2)*x(n+1)+(1/3)*x(n)+y(n);
end
however you have to change the stem arguments accordingly:
stem(0:1:49, y, 'r-')
  2 Commenti
Christopher Carey
Christopher Carey il 3 Dic 2017
thank you very much would you mind explaining how this fixes it please, I am a beginner at MATLAB.
Star Strider
Star Strider il 3 Dic 2017
As always, my pleasure.
You are indexing x(n+2) in your code, and ‘x’ has only 50 elements. In order to avoid reading beyond the number of elements in ‘x’, you need to reduce the number of iterations by 2. Setting the iterations to 2:length(x)-2 accomplishes that, since ‘n’ now goes from 2 to 48.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Signal Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by