Please help with Index exceeds matrix dimensions error

1 次查看(过去 30 天)
%%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;

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by