MATLAB Answers

0

Index exceeds number of array elements (181)

Asked by Tarek Zaqout on 11 Jun 2019
Latest activity Answered by Jan
on 11 Jun 2019
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 Comments

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))
Can you post down the the complete error statments

Sign in to comment.

1 Answer

Answer by Jan
on 11 Jun 2019

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 Comments

Sign in to comment.