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

1 vue (au cours des 30 derniers jours)
Mads
Mads le 2 Mar 2021
Commenté : Jan le 3 Mar 2021
%% 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 commentaires
Mads
Mads le 3 Mar 2021
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 le 3 Mar 2021
Thanks, Mads. I've removed the discussion.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 2 Mar 2021
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);

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by