# Array indices must be positive integer or logical values

조회 수: 1(최근 30일)
Anjana Ekka 2020년 9월 21일
답변: Abhishek Gupta 2020년 9월 24일
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표시 없음숨기기 없음
Walter Roberson 2020년 9월 21일
Suppose your input Tlag is negative...

댓글을 달려면 로그인하십시오.

### 답변(1개)

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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Operators and Elementary Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by