필터 지우기
필터 지우기

Index exceeds the number of array elements (1).

조회 수: 2 (최근 30일)
Sam Frank
Sam Frank 2020년 6월 14일
댓글: Sam Frank 2020년 6월 18일
t1=-1:0.1:0.5;
t2=0.6:0.1:3;
t= [t1 t2];
x1=0.6.*ones(size(t1));
x2=0.3.*ones(size(t2));
x=[x1 x2];
h=(exp(-t)).*(t>=0);
C=length(x)+length(h)-1;
X=0;
H=0;
for i=1: C
c(i) = 0;
for j=1:i
if(i-j+1>0)
c(i)= c(i)+ X(j).* H(i-j+1);
else
end
end
end
plot(c)
I'm trying to v=convolve x and h but everytime i run this it gives me the array size error.
I'm using MATLAB Online R2020a, and when I gave this code to someone who had RMATLAB2015a installed on their computer, there was no error.
error is in line 16:
c(i)= c(i)+ X(j).* H(i-j+1);
  댓글 수: 1
KSSV
KSSV 2020년 6월 14일
What is that code? It is full of mess. What exactly you are trying?

댓글을 달려면 로그인하십시오.

채택된 답변

Ayush Goyal
Ayush Goyal 2020년 6월 18일
편집: Ayush Goyal 2020년 6월 18일
From my understanding of the question you are trying to find the convolution of x and h but you are facing array size error. Error is due to different length of vectors x and h and you should transform the vectors x and h in new vectors X and H with the same length. Change the code for X and H and instead of iterating j from 1 to i iterate it from 1 to length(x)
t1=-1:0.1:0.5;
t2=0.6:0.1:3;
t= [t1 t2];
x1=0.6.*ones(size(t1));
x2=0.3.*ones(size(t2));
x=[x1 x2];
h=(exp(-t)).*(t>=0);
C=length(x)+length(h)-1;
X=[x,zeros(1,length(h))]; %zeropadding to have vectors of equal length
H=[h,zeros(1,length(x))]; %zeropadding to have vectors of equal length
for i=1: C
c(i) = 0;
for j=1:length(x) %Iterate it from 1 to length(x)
if(i-j+1>0)
c(i)= c(i)+ X(j).* H(i-j+1);
else
end
end
end
plot(c)
You can refer to the following link:
  댓글 수: 1
Sam Frank
Sam Frank 2020년 6월 18일
Thankyou! I'm still new to MATLAB and doing convolution no less. I didn't even know what zeropadding was :p. Thanks for helping out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by