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 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!