フィルターのクリア

Please help with Index exceeds matrix dimensions error

2 ビュー (過去 30 日間)
Christopher Carey
Christopher Carey 2017 年 12 月 3 日
コメント済み: Star Strider 2017 年 12 月 3 日
%%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 Center および File ExchangeSignal Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by