Index exceeds number of array elements (181)
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have this error while I'm using this code. I can't seem to find the source of the error.
for i=11:length(Tjord)
if nanmean(Tair(i)+Tair(i-1)+Tair(i-2)+Tair(i-3)+Tair(i-4)+Tair(i-5)+Tair(i-6)+Tair(i-7)+Tair(i-8)+Tair(i-9)+Tair(i-10))>10
Tjord(i)=Tjord(i-1)+(Tair(i)-Tair(i-3))*0.5;
elseif nanmean(Tair(i)+Tair(i-1)+Tair(i-2)+Tair(i-3)+Tair(i-4)+Tair(i-5)+Tair(i-6)+Tair(i-7)+Tair(i-8)+Tair(i-9)+Tair(i-10))>5
if Snowdepth(i)>0
Tjord(i)=Tjord(i-1)+(Tair(i)-Tair(i-5))*0.01;
else %no snow
Tjord(i)=Tjord(i-1)+(Tair(i)-Tair(i-3))*0.025;
end
elseif Tjord(i-1)==0
Tjord(i)=0;
else %WINTER; avg. Tair is below 2.5°C
if Snowdepth(i)>0
Tjord(i)=0;%Tjord(i-1)+(Tair(i)-Tair(i-5))*0.001;
else %frost and no snow
Tjord(i)=Tjord(i-1)+(Tair(i)-Tair(i-10))*0.001;
end
end
RMSE(i) = sqrt(mean((Tjord(i) - TIMETABLE_ALL_cal{i,3}).^2));
end
2 个评论
Torsten
2019-6-11
编辑:Torsten
2019-6-11
Maybe "Snowdepth" , "Tair" and/or "TIMETABLE_ALL_cal" have less elements than "Tjord" ?
And in the definition of "RMSE" you calculate the mean of a single element which looks strange to me.
Same for
nanmean(Tair(i)+Tair(i-1)+Tair(i-2)+Tair(i-3)+Tair(i-4)+Tair(i-5)+Tair(i-6)+Tair(i-7)+Tair(i-8)+Tair(i-9)+Tair(i-10))
回答(1 个)
Jan
2019-6-11
Use the debugger to examine the cause of the problems. Type this in the command window:
dbstop if error
Then run the code again. When it stops at the error. check the sizes of the variables used in the failing line.
By the way, simplify:
if nanmean(Tair(i)+Tair(i-1)+Tair(i-2)+Tair(i-3)+Tair(i-4)+Tair(i-5)+Tair(i-6)+Tair(i-7)+Tair(i-8)+Tair(i-9)+Tair(i-10))>10
Tjord(i)=Tjord(i-1)+(Tair(i)-Tair(i-3))*0.5;
elseif nanmean(Tair(i)+Tair(i-1)+Tair(i-2)+Tair(i-3)+Tair(i-4)+Tair(i-5)+Tair(i-6)+Tair(i-7)+Tair(i-8)+Tair(i-9)+Tair(i-10))>5
To
tmp = sum(Tair(i:-1:i-10));
if tmp > 10
...
elseif tmp > 5
...
The nanmean function is not useful here, because you provide a scalar as input only. Maybe you really want nanmean instead. Then:
tmp = nanmean(Tair(i:-1:i-10))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!