Array indices must be positive integer or logical values

1 vue (au cours des 30 derniers jours)
Anjana Ekka
Anjana Ekka le 21 Sep 2020
I am getting error .....Array indices must be positive integers or logical values
in line of code 15 (Weigths(i)=(1+(i-1)/th)*(th-floor(th))/2+(1+(Tlag-i)/th)*(floor(th)+1-th)/2)
would appreciate help!!
Thanks
function [ Weigths ] = Weigfun(Tlag)
%WEIGFUN Summary of this function goes here
nmax=ceil(Tlag);
if nmax==1
Weigths=1;
else
Weigths=zeros(1,nmax);
th=Tlag/2;
nh=floor(th);
for i=1:nh
Weigths(i)=(i-.5)/th;
end
i=nh+1;
Weigths(i)=(1+(i-1)/th)*(th-floor(th))/2+(1+(Tlag-i)/th)*(floor(th)+1-th)/2;
for i=nh+2:floor(Tlag)
Weigths(i)=(Tlag-i+.5)/th;
end
if Tlag>floor(Tlag)
Weigths(floor(Tlag)+1)=(Tlag-floor(Tlag)).^2/(2*th);
end
end
Weigths=Weigths/sum(Weigths);

Réponses (1)

Abhishek Gupta
Abhishek Gupta le 24 Sep 2020
Hi,
One plausible reason for you getting this error is that you might be calling the function with some negative value of ‘Tlag which results in a negative value of i’ variable (line 14). For example, if you run the code for Tlag = -5, then ‘i’ would be -2.
Referring to the following links, which will give you more information on indexing in MATLAB: -

Catégories

En savoir plus sur Operators and Elementary Operations 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