Error: Index exceeds the number of array elements (1).

조회 수: 1 (최근 30일)
Mads
Mads 2021년 3월 2일
댓글: Jan 2021년 3월 3일
%% Omitting NA's from intial dataset
data = rmmissing(Zerocoupondata);
data.SVENY05(12523) = 0.875;
%% Declaring variables:
Time = table2array(data(14:14600,1));% Time variable
Time = datenum(Time); % Declaring time as datenum
Y01 = table2array(data(14:14600,2)); % Bond of 1 year maturity
Y02 = table2array(data(14:14600,3)); % Bond of 2 years maturity
Y03 = table2array(data(14:14600,4)); % Bond of 3 years maturity
Y04 = table2array(data(14:14600,5)); % Bond of 4 years maturity
Y05 = table2array(data(14:14600,6)); % Bond of 5 years maturity
%% Realized Variance
% The variance through the whole sample period
Y05Var = (Y05(2:end) - Y05(1:end-1)).^2;
% Creating a loop to register timeshifts
%%
dates=Time;
g=1;
months = size(Y05Var,1);
years = size(Y05Var,1);
months(1)=month(dates(1));
years(1)=year(dates(1));
temp=1;
%%
for i=1:size(Y05Var,1)
if months(g) == month(dates(i)) & years(g) == year(dates(i));
else
var_monthly(g)=sum((Y05(temp+1:i-1)-Y05(temp:i-2)).^2);
g=g+1;
temp=i;
end
end
I get the error in the line "if months(g) == month(dates(i)) & years(g) == year(dates(i));". I hope you guys can help me. I am new to matlab. Thank you!
  댓글 수: 2
Mads
Mads 2021년 3월 3일
Ah okay, I totally understand and I am very sorry and will try tochange it back again. Thank you for noticing me Jan.
Jan
Jan 2021년 3월 3일
Thanks, Mads. I've removed the discussion.

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

채택된 답변

Jan
Jan 2021년 3월 2일
You have defined months as a scalar:
months(1) = month(dates(1));
Then months(g) must fail, if g>1.
I cannot guess, what you want to do instead, so it is not possible to suggest a solution. A bold guess:
% Replace: months(1) = month(dates(1)); by
months = month(dates);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by