Array indices must be positive integer or logical values

1 visualizzazione (ultimi 30 giorni)
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);

Risposte (1)

Abhishek Gupta
Abhishek Gupta il 24 Set 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: -

Categorie

Scopri di più su Operators and Elementary Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by