Array indices must be positive integer or logical values

1 次查看(过去 30 天)
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);

回答(1 个)

Abhishek Gupta
Abhishek Gupta 2020-9-24
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: -

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by