Index exceeds the number of array elements (1).
2 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Ayush Goyal
on 18 Jun 2020
Edited: Ayush Goyal
on 18 Jun 2020
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:
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!