Why do I get a for loop error "Subscript indices must either be real positive integers or logicals"?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have the following code (below) that uses a for loop. Why do I get the error "Subscript indices must either be real positive integers or logicals"?
data.Year=year(data.Date);
data.Month=month(data.Date);
data.Day=day(data.Date);
data.Hour=hour(data.Date)+1;
data.Wday=weekday(data.Date);
data.Bday=isbusday(data.Date);
data.Hday=1*((data.Bday==0)&((data.Wday~=1)&(data.Wday~=7)));
data.Hday2=data.Hday;
[rows, cols]=size(data.Hday);
for m=1:rows
if (data.Hday(m)==1)&&((data.Month(m)==12)||(data.Month(m)==11)||(data.Month(m)==9)|(data.Month(m)==7)|(data.Month(m)==5))
data.Hday2(m)=data.Hday(m)+1;
data.nday(m)=0;
elseif (data.Hday(m)==1)&&(data.Month(m)==1)&&(data.Day(m)<8)
data.Hday2(m)=data.Hday(m)+1;
data.nday(m)=0;
elseif (data.Hday(m)==1)
data.Hday2(m)=1;
data.nday(m)=0;
elseif (data.Hday(m-1)==1)&&(data.Month(m-1)==11)
data.Hday2(m)=.5
else
data.Hday2(m)=0;
data.nday(m)=1;
end
end
댓글 수: 0
채택된 답변
Geoff Hayes
2017년 10월 27일
The problem is because of m
for m=1:rows
Note how m starts at 1. Now look at this line
elseif (data.Hday(m-1)==1)&&(data.Month(m-1)==11)
You are subtracting 1 from m and so when m is one, then m-1 is zero which is not a real positive integer. MATLAB arrays are one-based and so the minimum index into an array is one.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!