Array indices must be positive integer or logical values

1 Ansicht (letzte 30 Tage)
Anjana Ekka
Anjana Ekka am 21 Sep. 2020
Beantwortet: Abhishek Gupta am 24 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);

Antworten (1)

Abhishek Gupta
Abhishek Gupta am 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: -

Kategorien

Mehr zu Operators and Elementary Operations 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