Borrar filtros
Borrar filtros

Please help with Index exceeds matrix dimensions error

2 visualizaciones (últimos 30 días)
%%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;

Respuesta aceptada

Star Strider
Star Strider el 3 de Dic. de 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 comentarios
Christopher Carey
Christopher Carey el 3 de Dic. de 2017
thank you very much would you mind explaining how this fixes it please, I am a beginner at MATLAB.
Star Strider
Star Strider el 3 de Dic. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Signal Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by