Effacer les filtres
Effacer les filtres

Index exceeds the number of array elements (1).

2 vues (au cours des 30 derniers jours)
Sam Frank
Sam Frank le 14 Juin 2020
Commenté : Sam Frank le 18 Juin 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 commentaire
KSSV
KSSV le 14 Juin 2020
What is that code? It is full of mess. What exactly you are trying?

Connectez-vous pour commenter.

Réponse acceptée

Ayush Goyal
Ayush Goyal le 18 Juin 2020
Modifié(e) : Ayush Goyal le 18 Juin 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 commentaire
Sam Frank
Sam Frank le 18 Juin 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by