Filter löschen
Filter löschen

Index exceeds the number of array elements (1).

2 Ansichten (letzte 30 Tage)
Sam Frank
Sam Frank am 14 Jun. 2020
Kommentiert: Sam Frank am 18 Jun. 2020
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 Kommentar
KSSV
KSSV am 14 Jun. 2020
What is that code? It is full of mess. What exactly you are trying?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ayush Goyal
Ayush Goyal am 18 Jun. 2020
Bearbeitet: Ayush Goyal am 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:
  1 Kommentar
Sam Frank
Sam Frank am 18 Jun. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by