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 Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by