Index exceeds the number of array elements (1).
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!