필터 지우기
필터 지우기

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 CenterFile Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by